Free Support Forum - aspose.com

Add PdfFileSignature in Existing Signature form field in Java

Hi Team,
I need to know “How I can add Signature in signature form field of existing PDF?”.
I am successfully add signature in new/existing PDF document but it creates new signature field but i want to add signature in my existing form field.
#Code:
Document document=new Document(“Test.pdf”);
OutputStream out = new java.io.ByteArrayOutputStream();
document.save(out);
PdfFileSignature sign = new PdfFileSignature();
sign.bindPdf(new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()));
pdfSignSingle.sign(field.getPageIndex(), true,new java.awt.Rectangle(500,500,500,500),new PKCS1(“test_sign.pfx”, “test12345”)); pdfSignSingle.setSignatureAppearance(“test_logo.jpeg”);
pdfSignSingle.save(“out.pdf”);

The above code create new signature field and add signature but i want to update signature in my existing form thatPDF FORM.pdf (34.2 KB)
i have shared.
Please review and let me know.
Thanks and Regards
Saurav Saxena

@sauravjava

You may please use following code snippet in order to add signatures to existing field:

Document doc = new Document(dataDir + "PDF FORM.pdf");
// if signature field name is knows
//SignatureField signField = (SignatureField)doc.getForm().get("SignatureFieldName");
for(Field field : doc.getForm().getFields()){
 if(field instanceof SignatureField){
    // Create PdfFileSignature instance
   com.aspose.pdf.facades.PdfFileSignature pdfSignSingle = new com.aspose.pdf.facades.PdfFileSignature();
   // Bind the source PDF by reading contents of Stream
   pdfSignSingle.bindPdf(doc);
   // Sign the PDF file using PKCS1 object
   PKCS7 pkcs = new PKCS7(dataDir + "mykey2.pfx", "aa");
   pkcs.setReason("");
   pkcs.setContactInfo("");
   pkcs.setDate(null);
   SignatureCustomAppearance sca = new SignatureCustomAppearance();
   sca.setDateSignedAtLabel(null);
   sca.setDigitalSignedLabel(null);
   sca.setShowReason(false);
   sca.setShowLocation(false);
   sca.setShowContactInfo(false);

   pkcs.setCustomAppearance(sca);
   pdfSignSingle.sign(1, true, field.getRectangle(true).toRect(), pkcs);
   // Set image for signature appearance
   pdfSignSingle.setSignatureAppearance(dataDir + "avatar.png");
   // Save final output
   pdfSignSingle.save(dataDir + "signed.pdf");
 }
}

Thank you for your support.
With Regards
Saurav Saxena

1 Like