How to merge PDFs without losing digital signature using Java (IText API)

Is it possible to attach a digitally signed PDF document to another normal PDF using the Java iText API? I have attempted to merge the PDFs, but the resulting output PDF does not retain the digital signature. I would like to know if it is indeed possible to preserve the digital signature in the final PDF file.

1

3 Answers

As others already have stated, the idea (at least a major part of the idea) behind signing is to make sure the document has not changed. Merging, on the other hand, does change the document. Thus, merging will break signatures.

A different approach would be, though, to make the other, "normal" PDF a portable collection (a special kind of PDF with attachments) and attach the signed PDF to that collection.

When opening the signed PDF from the collection, the signature will be as unharmed as in the original signed PDF.

Example code for creating a portable collection

You can find an example of portable collection creation on the iText site:

public static final String DEST = "results/collections/portable_collection.pdf"; public static final String DATA = "resources/data/united_states.csv"; public static final String HELLO = "resources/pdfs/hello.pdf"; public static final String IMG = "resources/images/berlin2013.jpg"; public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); document.add(new Paragraph("Portable collection")); PdfCollection collection = new PdfCollection(PdfCollection.TILE); writer.setCollection(collection); PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded( writer, DATA, "united_states.csv", null); writer.addFileAttachment("united_states.csv", fileSpec); fileSpec = PdfFileSpecification.fileEmbedded( writer, HELLO, "hello.pdf", null); writer.addFileAttachment("hello.pdf", fileSpec); fileSpec = PdfFileSpecification.fileEmbedded( writer, IMG, "berlin2013.jpg", null); writer.addFileAttachment("berlin2013.jpg", fileSpec); document.close(); } 

(here on the web site, here in their github)

A result of a run of that example is here.

(As you used the itext tag and not the itext7 tag, I assume you use an iText version 5.5.x.)

11
  1. Open the signed pdf in Adobe.

  2. Open print dialogue (Ctrl+P)

  3. Change the printer to "Microsoft Print to PDF" then print.

  4. The newly created PDF will have the signatures and will behave as a normal pdf for combine/merge activities.

    enter image description here

Note: This approach converts a signed document to a standard pdf. The result displays the signature information but the underlying digital signature is lost. In my case the original signers understand the distinction.

Creating a summary file is my goal. I combine a variety of digitally signed, along with other related, documentation into a single summary pdf. The original, digitally signed documents, are stored for future reference. I am increasingly convinced that it is not possible to combine digitally signed documents into a single, summary pdf, while maintaining the underlying digital signatures.

Users needing summary packets will benefit from my suggested approach. Keep in mind that my my approach remains "legally valid" to the extent that the original digitally signed documents are available on demand.

1

It is not possible, this digital signing is especially designed to protect the original document from being modified in any way.

To have this two documents merged and signed, you'd need to know the keys used for the signing and generate the signature once again for the new merged document.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like