I updated the code to create a widget/field when signing the document, using
Field sigField = doc.fieldCreate("Signature", Field.e_signature); Widget widgetAnnot = Widget.create(doc, new Rect(0, 100, 100, 0), sigField);
But the signature is not seen anywhere in the document. When opening the document with Acrobat Reader, it tells me that “There is at least one problem with a signature”.
Full code:
`
public static boolean signPDF(Document document) {
boolean result = true;
String outfile = “./signed_doc.pdf”;
String certfile = “./cert.p12”;
String imagefile = “./signature.jpg”;
try {
PDFDoc doc = new PDFDoc(FileSystem.getFileInputStream(document.getIdAlfrescoObject(), FolderEnum.DOCUMENTS));
// Add an StdSignatureHandler instance to PDFDoc, making sure to keep track of it using the ID returned.
long sigHandlerId = doc.addStdSignatureHandler(certfile, “password”);
Field sigField = doc.fieldCreate(“Signature”, Field.e_signature);
Widget widgetAnnot = Widget.create(doc, new Rect(0, 100, 100, 0), sigField);
// Tell PDFNet to use the SignatureHandler created to sign the new signature form field.
Obj sigDict = sigField.useSignatureHandler(sigHandlerId);
// Add more information to the signature dictionary
sigDict.putName(“SubFilter”, “adbe.pkcs7.detached”);
sigDict.putString(“Name”, “PDFTron”);
sigDict.putString(“Location”, “Vancouver, BC”);
sigDict.putString(“Reason”, “Document verification.”);
// Add the signature appearance
ElementWriter apWriter = new ElementWriter();
ElementBuilder apBuilder = new ElementBuilder();
apWriter.begin(doc);
Image sigImg = Image.create(doc, imagefile);
double w = sigImg.getImageWidth(), h = sigImg.getImageHeight();
Element apElement = apBuilder.createImage(sigImg, 0, 0, w, h);
apWriter.writePlacedElement(apElement);
Obj apObj = apWriter.end();
apObj.putRect(“BBox”, 0, 0, w, h);
apObj.putName(“Subtype”, “Form”);
apObj.putName(“Type”, “XObject”);
apWriter.begin(doc);
apElement = apBuilder.createForm(apObj);
apWriter.writePlacedElement(apElement);
apObj = apWriter.end();
apObj.putRect(“BBox”, 0, 0, w, h);
apObj.putName(“Subtype”, “Form”);
apObj.putName(“Type”, “XObject”);
widgetAnnot.setAppearance(apObj);
widgetAnnot.refreshAppearance();
// Save the PDFDoc. Once the method below is called, PDFNet will also sign the document using the information
// provided.
doc.save(outfile, SDFDoc.e_incremental, null);
doc.close();
System.out.println(“Finished signing PDF document.”);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
result = false;
}
return result;
}
`
There is no error thrown and every external file like the signature image or certificate is available.
Any idea?
Thanks