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.
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.
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.)