Q: How can I add a digital signature to a document using Qoppa’s java library jPDFSecure?
A: This Java program loads a document, then loads a digital ID from a PKCS#12 file, and then creates and signs a signature field on the first page of the document.
// Load the document PDFSecure pdfDoc = new PDFSecure ("input.pdf", null); // Load the keystore that contains the digital id to use in signing FileInputStream pkcs12Stream = new FileInputStream ("keystore.pfx"); KeyStore store = KeyStore.getInstance("PKCS12"); store.load(pkcs12Stream, "store_pwd".toCharArray()); pkcs12Stream.close(); // Create signing information SigningInformation signInfo = new SigningInformation (store, "key_alias", "key_pwd"); // Create signature field on the first page Rectangle2D signBounds = new Rectangle2D.Double (36, 36, 144, 48); SignatureField signField = pdfDoc.addSignatureField(0, "signature", signBounds); // Apply digital signature pdfDoc.signDocument(signField, signInfo); // Save the document pdfDoc.saveDocument ("output.pdf"); |
Download Full Java Sample Code to Sign a PDF
You can create a sample PKCS#12 file using Java’s keytool program. The command line to create a test file is as follows:
keytool -genkey -keystore test.pfx -storetype pkcs12 -storepass store_pwd -keypass key_pwd -alias key_alias