In order to sign a PDF document digitally using Aspose.PDF for Java, please check the example given in the following article of the API documentation:
I am using a self-sign certificate to test your code but I am not getting my expected output.
I attached my certificate and input document.
rabin.zip (3.5 KB)
My expected output file is given below.
LM1.pdf (196.2 KB)
Would you please share the password of the certificate that you have shared with us.
Also, we noticed that the signed PDF (LM1.pdf) was alright as the signature was valid in it while viewing it in Adobe Reader. Furthermore, we noticed that there were two stamps in the header of each page. Could you please confirm if you want to have an output with such stamps in header?
Yes, my output file will same as input file. Only the digital signature will be added inside the document.
“LM1.pdf” is my expacted output file. And “rabin.pdf” is my input file.
password.zip (130 Bytes)
Please check following code snippet that we used to sign the PDF document using provided certificate file.
String inputFile = dataDir + "rabin.pdf"; String outSignedFile = dataDir + "out_20.12.pdf"; // 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(inputFile); // Sign the PDF file using PKCS7 object PKCS7 pkcs = new PKCS7(dataDir + "sender_keystore.p12", "changeit"); pkcs.setAuthority("Authority"); pkcs.setDate(new Date()); pkcs.setContactInfo("email"); pkcs.setLocation("Location"); pkcs.setReason("reason"); //pkcs.setImage(new FileInputStream(dataDir + "simploudLogo.png")); SignatureCustomAppearance sca = new SignatureCustomAppearance(); sca.setDateSignedAtLabel(null); sca.setDigitalSignedLabel(null); sca.setShowReason(true); sca.setShowLocation(true); sca.setShowContactInfo(true); pkcs.setCustomAppearance(sca); pdfSignSingle.sign(1, true, new java.awt.Rectangle(100, 100, 200, 200), pkcs); // Set image for signature appearance //pdfSignSingle.setSignatureAppearance(dataDir + "simploudLogo.png"); // Save final output pdfSignSingle.save(outSignedFile);
out_20.12.pdf (41.5 KB)
Attached is the output PDF generated by the above code snippet. Would you please check it and let us know in case you notice any issue inside it.
I am not able to import "SignatureCustomAppearance ".
Because i am using bellow depaindency for aspose-pdf.
My licence is not support more then this version. Can you please help to write code for this version.
Please note that it is always recommended to use latest version of the API as it contains more enhancements and fixes. Please try to upgrade your license for the latest version in order to get benefits of latest updates. Furthermore, you can please remove the code part where Custom Appearance is used in the shared snippet and it will work with your existing API Version.
We did not notice this issue with 20.12 version of the API after removing the code for CustomAppearance. It seems like the issue is related to an older version of the API. Please try to upgrade the API to 20.12 and let us know in case you still face this issue. You can try 30-days free temporary license in order to evaluate the latest version.
In order to achieve that, you need to add your custom SignatureField in the PDF and specify its PartialName. Please check following code snippet:
String inputFile = dataDir + "rabin.pdf"; String outSignedFile = dataDir + "out_20.12.pdf"; Document doc = new Document(inputFile); SignatureField field1 = new SignatureField(doc.getPages().get_Item(1), new Rectangle(100, 100, 200, 200)); field1.setPartialName("PartialName"); field1.setName("Name"); field1.setAlternateName("AlternateName"); doc.getForm().add(field1, 1); PKCS7 pkcs = new PKCS7(dataDir + "sender_keystore.p12", "changeit"); pkcs.setAuthority("Authority"); pkcs.setDate(new Date()); pkcs.setContactInfo("email"); pkcs.setLocation("Location"); pkcs.setReason("reason"); //pkcs.setImage(new FileInputStream(dataDir + "simploudLogo.png")); SignatureCustomAppearance sca = new SignatureCustomAppearance(); sca.setDateSignedAtLabel(null); sca.setDigitalSignedLabel(null); sca.setShowReason(true); sca.setShowLocation(true); sca.setShowContactInfo(true); pkcs.setCustomAppearance(sca); field1.sign(pkcs); doc.save(dataDir + "Signed.pdf");
After adding the field my signature is not valid.
I attached my output file.
And I want to add filed for the document, not for the page.If I add filed for page then how i will add the same filed for every page.
Signed1.zip (70.2 KB)
Expected output file:
out_20.12 (2).zip (74.3 KB)
using iText i am able to achive this.
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
// set “signature1” for the document
appearance.setVisibleSignature(new Rectangle(10, 10, 10, 10), 1, “Signature1”);
// Creating the signature
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider);
List crlList = new ArrayList();
MakeSignature.signDetached(appearance, digest, signature, chain, crlList, null, null, 0, subfilter);
We have checked both files shared by you and noticed that they both were generated using Aspose.PDF for Java. Would you please share the code snippet using which they were created. We can see that you used different versions of the API to generate these files. Also, please share the certificate file using which you have signed them.
Do you mean that you want to add signatures on every page of the PDF file? OR you just need to sign PDF document and display some stamp on every page?