简体   繁体   English

如何使用PDFBox API在PDF表单字段中设置Signature

[英]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' . 实际上,我有大约20个表单字段的PDF,其中一个表单字段的类型为'PDSignatureField'。 Now I need to set a value to this PDSignatureField 现在,我需要为此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 ) 这是我尝试过的部分代码(我可以获取Signature值,但是当我尝试设置setSignature时,在保存文档后无法查看它)

GetSignature --> working fine GetSignature->运行正常

        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 SetSignature->无法查看特定表单字段中的签名值
here 'sigObject' is been declared as PDSignature object 在这里,“ sigObject”被声明为PDSignature对象

        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)之后添加以下代码片段,我快到了:

/* 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. 但是,我仍然有一个不希望看到的结果,其中两次显示了签名,一次是签名字段的值,另一次是另一个幻影字段“ Signature1”,但我还没有找到原因。

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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