I want to sign a PDF with multiple fields using the same SignatureHandler instance. When I do this and open the signed PDF, I am getting invalid signature errors. How can I sign a PDF with multiple fields using the same SignatureHandler?
Please be aware that most other PDF
readers out there does not allow for having more than one
field signed at the same time (in one save process).
PDFNet allows for this process and adds as many skips as
possible in byte ranges of data to sign as per the PDF
specifications (see the PDF reference for details). Most
PDF readers, however, does not validate a signature with
byte ranges like this. If you wish to sign multiple fields
in a document using PDFNet which will validate in most PDF
readers, you will need to save the PDF document each time
you call the method Field.useSignatureHandler. Then after
that, you will need to re-open the document, repeat the
signing process, then save again. It does not matter
whether you wish to use the same SignatureHandler instance
or not for all the signature fields you wish to sign. For
example:
PDFDoc doc = new PDFDoc("/path/to/doc.pdf");
sigHandlerId = doc.addSignatureHandler(mySigHandler);
Field sigField =
doc.getField("Signature1");
sigField.useSignatureHandler(sigHandlerId);
doc.save("/path/to/signed_doc1.pdf",
SDFDoc.e_incremental,
null);
// Re-open the doc
doc = new PDFDoc("/path/to/signed_doc.pdf");
// Re-add the signature handler...
sigHandlerId = doc.addSignatureHandler(mySigHandler);
// we can re-use the
existing SignatureHandler instance
sigField =
doc.getField("Signature2");
sigField.useSignatureHandler(sigHandlerId);
doc.save("/path/to/signed_doc2.pdf",
SDFDoc.e_incremental, null);