Add Digital Signature on Stamp Annotations

229 views
Skip to first unread message

ch.gru...@gmail.com

unread,
Dec 20, 2018, 5:44:31 PM12/20/18
to PDFTron PDFNet SDK
Hi, 

we would like to add Digital Signatures with .p12 certificates to our documents. Currently, we only add Electronic Signatures to the document using the PDFTron Webviewer. Our Electronic Signatures have the annotation type "stamp" and are created dynamically upon loading the Webviewer, since every user has its own signature. The signatures are pictures that are converted to Base64 when saving the document. 

Now, I tried to add an Digital Signature (Approval Signature) using your demo code (https://www.pdftron.com/documentation/samples/java/DigitalSignaturesTest). But the demo code only shows how to add a Digital Signature on form fields.

-> Is it possible to add Digital Signatures without such a form field but directly with a stamp annotation? Since we don't have form fields on the documents, we don't have any object of type "Field" (Field sigField = doc.getField("Signature1");)

Best regards, 

Christian Gruber


Shakthi Wijeratne

unread,
Jan 4, 2019, 6:31:41 PM1/4/19
to PDFTron PDFNet SDK
Hello, 

The PDF specification requires you to have a signature field for the digital signature to be applied to. It is possible to create this field using the SDK when the document is signing. For information on how to do this, you can refer to the following sample:


Just to get a better understanding of your requirements, may I ask why digitally signing the document is important to you?

ch.gru...@gmail.com

unread,
Jan 7, 2019, 2:29:40 PM1/7/19
to PDFTron PDFNet SDK
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

ch.gru...@gmail.com

unread,
Jan 7, 2019, 2:29:40 PM1/7/19
to PDFTron PDFNet SDK
Thanks for the link provided, I'll take a look.

Digitally Signing is just a additional feature of our application, there is no special requirement. We just want to add (more) authenticity and integrity to the document. We would use the same certificates as we use for Email encryption.

ch.gru...@gmail.com

unread,
Jan 14, 2019, 12:21:43 PM1/14/19
to PDFTron PDFNet SDK
I guess this post got forgotten!?

Shakthi Wijeratne

unread,
Jan 18, 2019, 7:53:27 PM1/18/19
to PDFTron PDFNet SDK
Hello, apologies for the delay in response. 

To create a signature annotation and field, you must do a bit more than just creating a field. For more information, please refer to the following forum post:

Please let me know if  this works for you, and if you have any further questions. 
Reply all
Reply to author
Forward
0 new messages