Search code examples
javaitext7

itext 7: Problem save digitally signed document (java)


I have a document that is digitally signed, and when creating a new document from the first, it loses the signature, here I show the code:

    String src = "C:\\sign\\testpdf\\signed_document.pdf";
    String dest = "C:\\sign\\testpdf\\modified_document.pdf";
    PdfReader pdfReader = new PdfReader(src);
    PdfWriter pdfWritter = new PdfWriter(dest);
    PdfDocument pdf2 = new PdfDocument(pdfReader, pdfWritter);
    pdf2.close();
 

Is there a way to keep the new document signed? thanks!

EDIT: The signature appears on the new document but as "unknown". That is,the name of the person who signed does not appear


Solution

  • To apply changes to a signed PDF and keep the signatures cryptographically valid, you have to create an incremental update. In iText lingo that means using append mode. In iText 7 you use append mode by specifying stamping properties accordingly:

    PdfDocument pdf2 = new PdfDocument(pdfReader, pdfWritter, new StampingProperties().useAppendMode());
    

    Beware, you only are allowed to apply a small set of changes to a signed PDF, read this answer.