简体   繁体   中英

How to setSignature in PDF Form Field using PDFBox api

Actually I have PDF with Form Fields of about 20, in that one form field is of type 'PDSignatureField' . Now I need to set a value to this PDSignatureField

here's piece code of what I tried (Im able to get the Signature value but when I try to setSignature m not able to view it after saving the document )

GetSignature --> working fine

        document = PDDocument.load(documents);
        PDAcroForm form = document.getDocumentCatalog().getAcroForm();
        PDField pdfFields;
        pdfFields = form.getField("EMPLOYEE SIGNATURE");
        if (pdfFields instanceof PDSignatureField)
        {
            PDSignatureField f3 = (PDSignatureField)form.getField("EMPLOYEE SIGNATURE");
            System.out.println(f3.getSignature().getName());
        }

SetSignature --> Not able to view the signature value in that particular Form Field
here 'sigObject' is been declared as PDSignature object

        document = PDDocument.load(documents);
        PDAcroForm form = document.getDocumentCatalog().getAcroForm();
        PDField pdfFields;
        pdfFields = form.getField("EMPLOYEE SIGNATURE");
        if (pdfFields instanceof PDSignatureField)
        {
            PDSignatureField f3 = (PDSignatureField)form.getField("EMPLOYEE SIGNATURE");
            sigObject.setName("Test");
            sigObject.setLocation("Test");
            sigObject.setReason("Test"); 
            sigObject.setSignDate(Calendar.getInstance());
            f3.setSignature(sigObject);
        }

can any one help me out pls Thanks

I have a similar question and I am almost there by adding the following code fragments after f3.setSignature(sigObject):

/* f3.setSignature(sigObject) only sets the V attribute of the signature field. You still
   need to call document.addSignature() to register the signature interface and call
   saveIncremental() to call the sign() method and generate the actual Signature Dictionary object       
*/

f3.getCOSObject().setNeedToBeUpdate(true);
document.addSignature(sigObject, this);
document.saveIncremental(fis, fos); /* as in pdfbox examples */

However, I still have an undesirable result where the signature is shown twice, once as the value of the signature field and one as another phantom field "Signature1" that I have not found out why yet.

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM