Create Digitally Signed PDF Documents

@tahir.manzoor
add digital signed to view the pdf as like “ThH0zHa.png” using java.
PFA
ThH0zHa.zip (57.2 KB)

@rabinintig

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:

Hi @asad.ali
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)

@rabinintig

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?

@asad.ali
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.
PFA
password file
password.zip (130 Bytes)

@rabinintig

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.

hi @asad.ali
I am not able to import "SignatureCustomAppearance ".
Because i am using bellow depaindency for aspose-pdf.

com.aspose
aspose-pdf
16.10.0

My licence is not support more then this version. Can you please help to write code for this version.

@rabinintig

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.

hi @asad.ali
After removing "Custom Appearance " my output file is given below.
But this is not my expected output.
out_20.12.zip (29.6 KB)
The green bar of the top of the document is not comming

@rabinintig

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.

Hi @asad.ali
Thanks for helping.
Now i am facing an issue with the “filed:signature1”.
How i can add my custom value place of “signature1”.
PFA
Capture.zip (37.5 KB)

@rabinintig

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");

Hi @asad.ali
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();
appearance.setReason(reason);
appearance.setLocation(location);
// 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();
crlList.add(new CrlClientOnline(chain));
MakeSignature.signDetached(appearance, digest, signature, chain, crlList, null, null, 0, subfilter);

@rabinintig

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?