Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:27:57+00:00 2026-06-17T09:27:57+00:00

Hello there I’m quite new to java and working with itext. The idea is

  • 0

Hello there I’m quite new to java and working with itext. The idea is to make digital signature in pdf. Source code is from here with some adjustments with paths..

package signaturefield;

public class Signaturefield {

 /** The resulting PDF */
public static String ORIGINAL = "c:/Users/example.pdf";
/** The resulting PDF */
public static String SIGNED1 = "c:/Users/signed_1.pdf";
/** The resulting PDF */
public static String SIGNED2 = "c:/Users/signed_2.pdf";

/** One of the resources. */
public static final String RESOURCE
    = "C:/Users/watermark.png";

/**
 * A properties file that is PRIVATE.
 * You should make your own properties file and adapt this line.
 */
public static String PATH = "c:/Users/key.properties";
/** Some properties used when signing. */
public static Properties properties = new Properties();
private PrivateKey pk;

/**
 * Creates a PDF document.
 * @param filename the path to the new PDF document
 * @throws DocumentException 
 * @throws IOException 
 */
public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("Hello World!"));
    PdfFormField field = PdfFormField.createSignature(writer);
    field.setWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_INVERT);
    field.setFieldName("mySig");
    field.setFlags(PdfAnnotation.FLAGS_PRINT);
    field.setPage();
    field.setMKBorderColor(BaseColor.BLACK);
    field.setMKBackgroundColor(BaseColor.WHITE);
    PdfAppearance tp = PdfAppearance.createAppearance(writer, 72, 48);
    tp.rectangle(0.5f, 0.5f, 71.5f, 47.5f);
    tp.stroke();
    field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
    writer.addAnnotation(field);
    // step 5
    document.close();
}

/**
 * Manipulates a PDF file src with the file dest as result
 * @param src the original PDF
 * @param dest the resulting PDF
 * @throws GeneralSecurityException 
 * @throws IOException 
 * @throws DocumentException 
 * @throws FileNotFoundException 
 * @throws KeyStoreException 
 * @throws Exception 
 */
public void signPdf(String src, String dest, boolean certified, boolean graphic) throws GeneralSecurityException, IOException, DocumentException {
    // private key and certificate
    String path = properties.getProperty("PRIVATE");
    String keystore_password = properties.getProperty("PASSWORD");
    String key_password = properties.getProperty("PASSWORD");
    KeyStore ks = KeyStore.getInstance("pkcs12", "BC");
    ks.load(new FileInputStream(path), keystore_password.toCharArray());
    String alias = (String)ks.aliases().nextElement();
    PrivateKey pk = (PrivateKey)ks.getKey(alias, key_password.toCharArray());
    Certificate[] chain = ks.getCertificateChain(alias);
    // reader and stamper
    PdfReader reader = new PdfReader(ORIGINAL);
    PdfStamper stamper = PdfStamper.createSignature(reader, new FileOutputStream(dest), '\0');
    // appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setVisibleSignature("mySig");
    appearance.setReason("It's personal.");
    appearance.setLocation("Foobar");
    if (certified) {
        appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
    }
    if (graphic) {
        appearance.setSignatureGraphic(Image.getInstance(RESOURCE));
        appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
    }
    // signature
    ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
    ExternalDigest digest = new BouncyCastleDigest();
    MakeSignature.signDetached(appearance, digest, es, chain, null,null, null, 0, CryptoStandard.CMS);
}


public static void main(String[] args)
    throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    properties.load(new FileInputStream(PATH));
    Signaturefield signatures = new Signaturefield();
    signatures.createPdf(ORIGINAL);
    signatures.signPdf(ORIGINAL, SIGNED1, false, false);
    signatures.signPdf(ORIGINAL, SIGNED2, true, true);
}
}

after running this i get error:

Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:134)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at signaturefield.Signaturefield.signPdf(Signaturefield.java:114)
    at signaturefield.Signaturefield.main(Signaturefield.java:146)
Java Result: 1

Any idea of fixing it? thanks alot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:27:58+00:00Added an answer on June 17, 2026 at 9:27 am

    That means that you path variable is null in signPdf method. Properties file which you are loading don’t have property named PRIVATE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there i have written some code in jquery to add new input type
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Hello there Stack Exchange, I'm trying to use a Raspberry Pi to make a
Hello there here is my issue. This is my running code, which is fine:
Hello there im trying to send files using client-server classes in java. For some
hello there is something i've realized with quartz when working.Say a cron is set
Hello there I am working on an assignment in C where I need to
Hello there i would like to know if it is possible to make a
Hello there I have the following code: struct Edge { Node * nodeA, *
Hello There, I am new to phonegap.I am trying to record a audio clip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.