I have a PDF document that is digitaly signed by an application other than pdfbox. I would now like add an overlay to that digitaly signed PDF using pdfbox. Usually, this would look something like this:
Overlay overlayObj = new Overlay();
PDDocument originalDoc = PDDocument.load(new File(...));
PDDocument overlayDoc = PDDocument.load(new File(...));
overlayObj.setInputPDF(originalDoc);
overlayObj.setAllPagesOverlayPDF(overlayDoc );
Map<Integer, String> ovmap = new HashMap<Integer, String>(); // empty map is a dummy
PDDocument res = overlayObj.overlay(ovmap);
res.save(...);
However, this code generates a new PDF file, thus invalidating the digital signature. It seems that the saveIncremental()
(https://issues.apache.org/jira/browse/PDFBOX-45) may be able to preserve the digital signature.
Is there a way to use saveIncremental()
in combination with overlays? Or is there any other way to overlay a file with a digital signature that preserves the signature?
When you apply a regular overlay to a signed PDF, i.e. when you add the static content of another PDF to the static content of the signed PDF, you will always invalidate the signature.
This is because only certain changes are allowed to a signed PDF, and changing the static content is not one of them. For details see this answer and documents referenced from there.
Whether there is an alternative, depends on the nature of the signature in the PDF. If it restricts the allowed changes to form fill-in only or even forbids changes at all, you're out of luck.
But if the signature allows annotation changes, you can try and add the overlay content as a page filling annotation. Obviously the annotation must be added in an incremental update. Beware, though, this overlay-in-an-annotation acts differently from regular overlay content. E.g. regular text extraction will not extract text from the overlay. Also the annotation is very volatile, it can easily be removed, even accidentally.