Add a Digital Signature to a PDF - Java

This tutorial shows how to add a digital signature certificate to a PDF file and read back the signature information in a Java application using the LEADTOOLS SDK. A certificate-based digital signature adds security to the document by guaranteeing that the document has not been modified.

Summary This tutorial covers how to digitally sign a document in a Java application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Java Application
IDE Eclipse / IntelliJ
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Extract Attachments from a PDF - Java tutorial.

Digital Signature

Digital signatures provide document security by using a PFX encryption file to authenticate the signer's identity.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by local .jar files located at <INSTALL_DIR>\LEADTOOLS22\Bin\Java.

For this project, the following references are needed:

For a complete list of which JAR files are required for your application, refer to Files to be Included with your Java Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:


Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Sign Document Code

With the project created, the references added, and the license set, coding can begin.

Open the class in the Package Explorer. Add the following statements to the import block at the top.

package java_digitally_sign_a_pdf; 
import java.nio.file.*;  
import leadtools.*; 
import leadtools.pdf.*; 

Inside the run() method, add the following to set the library path to where the C DLL files are located, as well as load the LEADTOOLS libraries that were previously imported.

private void run(String[] args) {  
	try {  
		String inputFile = "SOURCE PDF FILE PATH"; 
		String outputFile = "FILE PATH TO OUTPUT SIGNED PDF TO"; 
		String signatureFile = "FILE PATH TO PFX SIGNATURE FILE"; 
		if (CheckDigitalSignatureSupportStatus() == false) 
			System.out.println("Digital Signature functionality is not available. " 
				+ "Make sure you have the Open SSL libs: " 
			   + ""); 
		SignPDFDocument(inputFile, outputFile, signatureFile, "password"); 
	catch(Exception ex) {  

Inside the _Main class, add a new method named CheckDigitalSignatureSupportStatus(). Call this method inside the run() method in a conditional statement, as shown above. Add the code below to the new method to check the digital signature support status.

private static Boolean CheckDigitalSignatureSupportStatus() 
	return PDFDocument.getDigitalSignatureSupportStatus() == RasterExceptionCode.SUCCESS; 


Java does not need to use the SetResourcesDirectory call to load the OpenSSL binaries, as in .NET, because the Platform.setLibPath does this already.

If you do not have the LEAD-Compiled OpenSSL binaries, you can download them here.

Add two more methods to the _Main class named SignPDFDocument(String inputPdf, String outputPdf, String signatureFile, String password) and ParsePDF(String filename). Both of these methods will be called inside the run() method below the call to the SetLicense() and String variables as shown above.

Add the code below to the SignPDFDocument() method to create a new PDFFile, digitally sign the PDF, and export it to the file path specified.

private static void SignPDFDocument(String inputPdf, String outputPdf, String signatureFile, String password) { 
	try	{ 
		PDFFile inputDoc = new PDFFile(inputPdf); 
		inputDoc.signDocument(outputPdf, signatureFile, password); 
	} catch (Exception ex) { 
	System.out.println("Signed PDF successfully"); 

Add the code below to the ParsePDF() method to parse the signature inside the PDF, if there is one.

private static void ParsePDF(String filename) { 
	PDFDocument doc = new PDFDocument(filename); 
	doc.parsePages(PDFParsePagesOptions.SIGNATURES.getValue(), 1, -1); 
	System.out.println(); // Act as a spacer 
	for (PDFDocumentPage page : doc.getPages()){ 
		System.out.printf("Page %d\n\n", page.getPageNumber()); 
		if (page.getSignatures().toArray().length == 0) 
			System.out.println("No signatures on this page\n"); 
		for (PDFSignature sig : page.getSignatures()) { 

Add a new method to the _Main class named PrintSignatureDetails(PDFSignature signature). Be sure to call this method at the bottom of the ParsePDF method, as shown above. Add the code below to the PrintSignatureDetails() to print out the extracted information from the PDFSignature class.

private static void PrintSignatureDetails(PDFSignature signature) { 
	System.out.printf("Issuer: %s\n", signature.getIssuer()); 
	System.out.printf("Public Key: %s\n", signature.getPublicKey()); 
	System.out.printf("Serial Number: %s\n", signature.getSerialNumber()); 
	System.out.printf("Subject: %s\n", signature.getSubject()); 
	System.out.printf("Valid start date: %s\n", signature.getValidFrom()); 
	System.out.printf("Valid end date: %s\n", signature.getValidTo()); 
	System.out.printf("Version: %s\n", signature.getVersion()); 
	System.out.println(); // Act as a spacer 

Run the Project

Run the project by pressing Ctrl + F11, or by selecting Run -> Run.

If the steps were followed correctly, the application runs, signs the given PDF with the given digital signature, and then prints out the signature details to the console.


This tutorial showed how to sign a PDF using a digital signature certificate file, and showed how to parse values from a document's digital signature. It also covered how to use the PDFFile, PDFDocument, PDFSignature, and PDFDocumentPage classes.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.