add digital signature to PDF Java

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. To digitally sign the PDF documents programmatically, we’ll show you how to add digital signatures to PDF using Java. Also, you’ll learn how to verify a digitally signed PDF programmatically.

We’ll cover the following scenarios related to digital signatures in PDFs.

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>22.12</version>
</dependency>

Add Digital Signature to a PDF in Java

The following are the steps to add a digital signature to a PDF in 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.

Java: Digitally Sign PDF with a Timestamp Server

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.

The following code sample shows how to verify a digitally signed PDF document using Java.

Free Java Library for Digital Signatures in PDF

You can get a free temporary license and add digital signatures to PDF files without any limitations.

Explore Java PDF Library

You can learn more about Java PDF API using the documentation as well as the source code samples hosted on GitHub.

Conclusion

In this article, you have learned how to add a 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 easily install the library and use the provided code samples in your application.

See Also