Here is some sample code showing how to use Qoppa’s Java PDF library jPDFSecure to apply 2 digital signatures to the same document or in other words, to sign a PDF document twice.

This code can be very simply adpated to use our other library jPDFProcess and a PDFDocument object instead of a PDFSecure object.

public class SignTwice {
	private static final String IN = "C:/mydoc.pdf";
	private static final String KEYSTORE = "C:/keystore.p12";
	private static final String KEYSTORE_PASSWORD = "password";
	private static final String ALIAS = "alias";
	private static final String ALIAS_PASSWORD = "password";
	private static final String OUT = "C:/signedTwice.pdf";
	private static final String KEY = "000000000";
 
public static void main (String [] args)
{
    try
    {
       // Load the document
       PDFSecure pdfDoc = new PDFSecure (IN, null);
       PDFSecure.setKey(KEY);
 
       // Load the keystore that contains the digital id to use in signing
       FileInputStream pkcs12Stream = new FileInputStream (KEYSTORE);
       KeyStore store = KeyStore.getInstance("PKCS12");
       store.load(pkcs12Stream, KEYSTORE_PASSWORD.toCharArray());
       pkcs12Stream.close();
 
       // Create signing information
       SigningInformation signInfo = new SigningInformation (store, ALIAS, ALIAS_PASSWORD);
 
       // Create signature field on the first page
       Rectangle2D signBounds = new Rectangle2D.Double (36, 36, 144, 48);
       SignatureField signField = pdfDoc.addSignatureField(0, "signature1", signBounds);
 
        // Apply digital signature
        pdfDoc.signDocument(signField, signInfo);
 
        // Save the document & reload the signed document before applying a 2nd signature
         pdfDoc.saveDocument (OUT);
         pdfDoc = new PDFSecure(OUT, null);
 
         // Create another signature field on the first page
         signBounds = new Rectangle2D.Double (36, 100, 144, 48);
         signField = pdfDoc.addSignatureField(0, "signature2", signBounds);
 
        // Apply digital signature
        pdfDoc.signDocument(signField, signInfo);
 
        // Save the document that has been signed twice
        pdfDoc.saveDocument (OUT);
   }
   catch (Throwable t)
   {
     t.printStackTrace();
   }
 }
}

There are a few things worth noting:

  • First, it is important to set the license key, otherwise the signatures will be invalidated due to the document having been modified since a demo version watermark is applied to the pages of the document when the library is not running in full production mode.
  • Second, this sample applies 2 signature with the same SignatureInformation and digital ID. In practice, you would not need to apply two signatures with the same ID of course. Even if the document had multiple pages, you would only need to apply one signature as the hash created to secure the document applies to the whole content, and to all pages contained in the PDF document. When applying 2 signatures, it’s because you are signing on behalf of 2 different entities or persons.
  • Finally, note that after the first signature, the document needs to be reloaded before applying the second signature. This is because the process of signing happens when the file is saved.

Download Full Java Sample – Apply 2 signatures to a PDF