I create a pdf document and specify few acro-fields. These acro-fields used to be filled by java itext library. This document is digitally signed after adding all required acro-fields.
We already set form filling property in digital signature but whenever we are trying to fill this documents by itext library, this document's digital signature becomes invalid.
Here is the code that I am using to fill this document -
String FILE = "/Users/mahensha/Desktop/NOC/test1.pdf";
PdfReader reader = new PdfReader("/Users/mahensha/Desktop/NOC/test.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(FILE));
AcroFields form = stamper.getAcroFields();
System.out.println("form: " + form);
form.setField("SellerName", "Mr.Mahendra Kumar Sharma");
form.setField("AddressLine1", "My address");
stamper.setFormFlattening(true);
stamper.close();
reader.close();
I am using lowagie itext library for filling pdf form.
Is there any way to fix this issue. Thanks.
You create the PdfStamper
like this:
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(FILE));
This asks iText to take the original PDF, throw away unneeded parts, arrange the remainder as it sees fit, apply the additions you want, and save all this.
Such a procedure obviously breaks any existing signatures.
You can instruct iText to instead apply changes as incremental update, i.e. copy the existing pdf and append changes in a new revision. The resulting files usually are larger than the ones created as above but the pre-existing signatures are not broken mathematically.
You can request this append mode using the 4 parameter constructor of PdfStamper
, e.g. like this:
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(FILE), '\0', true);
Furthermore, you request form flattening:
stamper.setFormFlattening(true);
This automatically invalidates pre-existing signatures. Thus, don't do this.