Digital Signature in PDF lets you secure the document before it is shared with the stakeholders. Forging in the content of a digitally signed PDF document can easily be detected and verified. In order to digitally sign the PDF documents programmatically, I’ll show you how to add and verify digital signatures in PDF using Java. We’ll cover the following scenarios related to digital signatures in PDFs.
- Java Library to Digitally Sign PDF
- Add Digital Signatures to PDF using Java
- Digitally Sign PDF with a Timestamp Server using Java
- Verify Digital Signature in a PDF using Java
Java Library to Digitally Sign PDF
For adding and verifying digital signatures in PDF, we’ll be using Aspose.PDF for Java. Along with other PDF manipulation features, Aspose.PDF for Java lets you add and verify digital signatures in PDFs seamlessly. You can download the JAR of the API or get it installed in your Maven-based application.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.6</version>
</dependency>
Add Digital Signature to a PDF using Java
The following are the steps to add a digital signature to a PDF document using Aspose.PDF for Java.
- Create an instance of the Document class and initialize it with the PDF document’s path.
- Initialize the PdfFileSignature class and pass to it the Document object.
- Create an instance of PKCS7 class and initialize it with a certificate’s path and the password.
- Initialize DocMDPSignature class for the MDP signature type.
- Define a Rectangle to place the signature on the document’s page.
- Digitally sign PDF document using PdfFileSignature.Certify() method.
- Save the signed PDF using PdfFileSignature.Save() method.
The following code sample shows how to digitally sign a PDF using Java.
Digitally Sign PDF with a Timestamp Server using Java
Aspose.PDF for Java also lets you add digital signatures to PDF with the TimeStamp server. The TimestampSettings class is used for this purpose. The following code sample shows how to add digital signature to a PDF with a TimeStamp server using Java.
Verify Digital Signature in a PDF using Java
When you receive a digitally signed PDF, you can verify its signature quite easily. The following are the steps to verify a digitally signed PDF.
- Create an instance of the PdfFileSignature class.
- Bind the PDF file using PdfFileSigntature.bindPdf(string) method.
- Verify the validity of the signature using PdfFileSignature.verifySigned() method.
The following code sample shows how to verify a digitally signed PDF document using Java.
Conclusion
In this article, you have learned how to add digital signature to a PDF using Java. In addition, we have seen how to verify a digitally signed PDF file within a few steps. You can learn more about Java PDF API using the documentation as well as the source code samples hosted on GitHub.