How to Add Digital Signatures to PDFs in Java
In this article, you will learn how to add a digital signature to a PDF using IronPDF in a Java program.
IronPDF - A Java PDF Library
IronPDF Java PDF Library Overview is a Java library that allows developers to create new PDF documents from scratch through Converting HTML String to PDF, Converting HTML File to PDF, Word to PDF Conversion, or URL. It helps manipulate PDFs easily by adding or removing content from them. It provides Security and Digital Signing Options for the PDF document along with digital signing.
IronPDF does not require any other third-party libraries to perform any of its PDF-related tasks. It also provides the facility to convert between different file formats. It provides cross-platform support and is specifically designed for Java, Scala, and Kotlin.
Steps to Add Digital Signatures to PDF Document in Java
Prerequisites
To digitally sign PDF documents, the following prerequisites are required:
- Any Java IDE (NetBeans, Eclipse, IntelliJ) Installed - This article will use IntelliJ. You can separately download the Java Development Kit from the Oracle Java SE Downloads.
- Maven Java Project for Automation - This will help download and manage dependencies in your Java program. You can download Maven from the Apache Maven Download Page.
- IronPDF Library Downloaded and Installed - To download and install IronPDF, include its Maven dependency artifact. You can add the following code snippet in the
pom.xml
file dependencies tag:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2023.2.0</version>
</dependency>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2023.2.0</version>
</dependency>
Another dependency required is Slf4j-simple. It is optional and can be added using the following dependency in the
pom.xml
file:<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.5</version> </dependency>
XML
pom.xml file
IronPDF Import Java Packages
The following imports are required in the main file. It allows using IronPDF functions to add a digital signature to a PDF document:
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.signature.Signature;
import com.ironsoftware.ironpdf.signature.SignatureManager;
import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.signature.Signature;
import com.ironsoftware.ironpdf.signature.SignatureManager;
import java.io.IOException;
import java.nio.file.Paths;
Now, everything required is done and ready to add a digital signature to PDF documents in Java using IronPDF.
Create PDF File or Open Existing PDF File
To Learn How to Digitally Sign PDFs, it is necessary to either create a PDF document from scratch or open an existing PDF document. This article will create a new PDF document for PDF signature using the renderHtmlAsPdf Method
of the PdfDocument Class
. The code goes as follows:
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
The PDF document is created but not saved yet. So, the next step is to create a digital signature and then add it to this PDF document and then finally save it.
Create a Digital Signature
You will have to create a digital signature certificate using Adobe Reader, which will be used in Java code to digitally sign PDF documents with those details. You can have a look at How to Create a Digital Signature in Adobe Reader.
Open Adobe Reader and click on Edit > Preferences. Then click on Signatures and like more on Identities and Trusted Certificates.
The project preferences
Add the following Signature field to make the .pfx or .p12 file doc.
Add Digital ID dialog
The following code sample will create a new signature in Java code using the .pfx certificate file for Windows and .p12 file for Mac along with its private key (password):
Signature signature = new Signature("Iron.pfx", "123456");
Signature signature = new Signature("Iron.pfx", "123456");
Sign PDF File with Digital Signature
IronPDF helps with adding the signature certificate using the SignatureManager
class. [getSignature Method
](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#getSignature()) helps to get the previous signature of the PDF document, and then a new signature can be added using the SignPdfWithSignature Method
with the signature file as an argument. The code goes as follows:
SignatureManager signatureManager = pdf.getSignature();
signatureManager.SignPdfWithSignature(signature);
SignatureManager signatureManager = pdf.getSignature();
signatureManager.SignPdfWithSignature(signature);
Save PDF Signature File
Finally, let's save the PDF file; otherwise, the PDF is not signed by using the saveAs Method
of the PdfDocument
class. The code is simple and as follows:
pdf.saveAs("signed.pdf");
pdf.saveAs("signed.pdf");
On successful compilation and execution of the code, the output produces a PDF that has been digitally signed.
The output PDF file
IronPDF also provides other signing options, which are optional and can also be used for digital signatures. It includes signature images in the form of handwriting, computer-generated text images, or digitized images. The following code sample helps you add some additional security options for digital signing:
signature.setSigningContact("support@ironsoftware.com");
signature.setSigningLocation("Chicago, USA");
signature.setSigningReason("To show how to sign a PDF");
BufferedImage signatureImage = ImageIO.read(new File("handwriting.png"));
WritableRaster raster = signatureImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
signature.setSignatureImage(data.getData());
signature.setSigningContact("support@ironsoftware.com");
signature.setSigningLocation("Chicago, USA");
signature.setSigningReason("To show how to sign a PDF");
BufferedImage signatureImage = ImageIO.read(new File("handwriting.png"));
WritableRaster raster = signatureImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
signature.setSignatureImage(data.getData());
The complete code goes as follows:
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.signature.Signature;
import com.ironsoftware.ironpdf.signature.SignatureManager;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
public class DigitalSignatureExample {
public static void main(String[] args) throws IOException {
// Step 1. Create a PDF File Doc
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
Signature signature = new Signature("Iron.pfx", "123456");
// Step 3. Sign the PDF with the PdfSignature.
SignatureManager signatureManager = pdf.getSignature();
signatureManager.SignPdfWithSignature(signature);
// Step 4. The PDF is not signed until saved to file, stream, or byte array.
pdf.saveAs("signed.pdf");
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.signature.Signature;
import com.ironsoftware.ironpdf.signature.SignatureManager;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
public class DigitalSignatureExample {
public static void main(String[] args) throws IOException {
// Step 1. Create a PDF File Doc
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>");
// Step 2. Create a Signature.
Signature signature = new Signature("Iron.pfx", "123456");
// Step 3. Sign the PDF with the PdfSignature.
SignatureManager signatureManager = pdf.getSignature();
signatureManager.SignPdfWithSignature(signature);
// Step 4. The PDF is not signed until saved to file, stream, or byte array.
pdf.saveAs("signed.pdf");
}
}
You can check more information on Digital Signature from the PDF Signatures Code Example. If you want to add more security and even edit metadata, then check the Security and Metadata Code Example.
Conclusion
This article explained step by step how to add digital signatures to PDF documents using IronPDF for Java.
First, the article covered the necessary components required to work with PDFs in Java using IronPDF. Then created a simple PDF document using an HTML string. A certificate file was created from Adobe Reader, which was used to sign the original PDF document. IronPDF provides a Signature
and SignatureManager
class, which helps users add a certificate and sign PDF with a signature field.
IronPDF facilitates developers in carrying out PDF-related tasks with ease and speed. It provides accurate results with pixel-perfect PDFs. For a versatile and widely used programming language like Java, IronPDF is well-suited to work with PDF documents as it is also versatile and supports almost all PDF operations. Moreover, it is built on already successful .NET capabilities.
IronPDF also provides easy conversion from different file formats, using the fast IronPDFEngine
designed specifically for Java. You can use the IronPDF library to Extract or Add Text, Load and Extract Images, Add Headers and Footers to PDFs, annotations, Render Charts and Graphs to PDFs, work with PDF Forms, and many more.
You can get detailed information on how to use IronPDF from the Java PDF Code Examples pages.
IronPDF is free for individual development and provides a free trial without watermark to generate PDF documents. It can be licensed for commercial use, and its users can get more information on the license from this Detailed Licensing Information Page.
Frequently Asked Questions
What is a Java library for PDF manipulation?
IronPDF is a Java library that allows developers to create, manipulate, and digitally sign PDF documents. It provides features like converting HTML to PDF, adding or removing content, and digital signing without requiring third-party libraries.
How do I add a digital signature to a PDF in Java?
To add a digital signature, you need to use the IronPDF library to create or open a PDF document, create a digital signature with a certificate file, and apply the signature using the SignatureManager class. Finally, save the signed PDF document.
What are the prerequisites for digitally signing PDF documents in Java?
Prerequisites include having a Java IDE like IntelliJ, a Maven project for dependency management, and the IronPDF library. Additionally, a digital signature certificate created in Adobe Reader is required.
Can a Java library convert file formats other than HTML to PDF?
Yes, IronPDF can convert various file formats to PDF, such as Word documents and URLs, in addition to HTML strings and files.
Does a Java PDF library require any third-party libraries to work?
No, IronPDF does not require any third-party libraries for its core PDF-related functionalities.
How can I create a digital signature certificate for PDF signing?
You can create a digital signature certificate using Adobe Reader by navigating to Edit > Preferences > Signatures, and then managing Identities and Trusted Certificates.
What is the role of a class used to manage digital signatures in a Java library?
The SignatureManager class in IronPDF is used to manage digital signatures on PDF documents. It allows users to add new signatures and retrieve existing ones from the document.
What additional security options does a Java library provide for digital signing?
IronPDF allows adding signature images, specifying signing contact, location, and reason, and includes options for handwriting and digitized images as part of the digital signature.
Is a Java PDF library free to use?
IronPDF is free for individual development and provides a free trial without a watermark for generating PDF documents. It can be licensed for commercial use.
What are some additional features of a Java PDF manipulation library?
IronPDF allows text and image extraction, adding headers and footers, rendering charts and graphs, working with PDF forms, and more.