Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In this article, you will learn how to add a digital signature to a PDF using IronPDF in a Java program.
IronPDF is a Java library that allows developers to create new PDF documents from scratch through HTML string, HTML file, Word file, or URL. It helps manipulate PDFs easily by adding or removing content from them. It provides security 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.
To digitally sign PDF documents, the following prerequisites are required:
pom.xml
file dependencies tag:<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2024.9.1</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>
pom.xml file
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;
Now, everything required is done and ready to add a digital signature to PDF documents in Java using IronPDF.
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>");
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.
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.
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");
IronPDF helps with adding the signature certificate using the SignatureManager
class. [getSignature
](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#getSignature()) method 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);
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");
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());
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 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 example code here.
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, annotations, render charts and graphs, work with PDF forms, and many more.
You can get detailed information on how to use IronPDF from the Code Examples pages.
IronPDF is free for individual development and provides a free trial to generate PDF documents without a watermark. It can be licensed for commercial use, and its users can get more information on the license from this detailed licensing page.
9 .NET API products for your office documents