USING IRONPDF FOR JAVA

How to Add Digital Signatures to PDFs in Java

Updated March 26, 2023
Share:

Portable Document Format (PDF) is a digital document which is used to share data online. It has become a universal format for sharing text, images, reports, invoices, bills, business contracts and editable forms. With all this data sharing over the internet comes PDF documents security. Digital signatures and other document security options like password, authenticity and ownership rights are also equally important when working with PDF documents.

There are times when Java developers have to embed PDF functionalities in software applications and working with PDF files especially to modify it can be difficult. Similarly, adding signature details to a PDF document can also be difficult in Java. IronPDF helps to manipulate PDF files and add digital signatures quite easily and efficiently.

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 is a Java library which allows developers to create new PDF documents from scratch through HTML string, HTML file doc, or URL. It helps to manipulate PDFs easily by adding or removing content from it. 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.

Steps to Add Digital Signatures to PDF Document in Java

Prerequisites

To digitally sign PDF documents, following prerequisites are required:

  • Any JAVA IDE (Netbeans, Eclipse, IntelliJ) Installed- Here I will use IntelliJ. You can separately download the Java Development Kit from the Oracle website.
  • Maven Java Project for Automation - This will help download and manage dependencies in your Java program. You can download Maven from Apache Maven website.
  • 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>com.ironsoftware</artifactId>
   <version>2024.6.2</version>
</dependency>
XML
  • Another dependency required is Slf4j-simple. It is optional and can be added using the following dependency in pom.xml file:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.5</version>
    </dependency>
    XML
pom.xml file

pom.xml File

IronPDF Import Java Packages

The following imports are required in the main file. It allows to use IronPDF functions to add digital signature to 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;
JAVA

Now, everything required is done and we are ready to add Digital signature to PDF documents in Java using IronPDF.

Create PDF File or Open Existing PDF File

To digitally sign PDFs, we first need to either create a PDF document from scratch or open an existing PDF document. Here I am going to 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>");
JAVA

The PDF document is created but not saved yet. We will now 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 that details. You can have a look at how to create a digital signature here.

Open Adobe Reader and click on Edit > Preferences. Then click on Signatures and like more on Identities and Trusted Certificates.

Add the following Signature field to make the .pfx or .p12 file doc.

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");
JAVA

Sign PDF File with Digital Signature

IronPDF helps with adding the signature certificate using the SignatureManager class. getSignature method helps to get the previous signature of the PDF document and then 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);
JAVA

Save PDF Signature File

Finally, we have to save the PDF file otherwise the PDF is not signed. We will save the PDF document using the saveAs method of the PdfDocument class. The code is simple and as follows:

pdf.saveAs("signed.pdf");
JAVA

On successful compilation and execution of the code, the output produces a PDF that has been digitally signed.

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. 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());
JAVA

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");
}
JAVA

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.

Conclusion

This article explained step by step, how to add digital signatures to PDF documents using IronPDF for Java.

First, we covered the necessary components required to work with PDFs in Java using IronPDF. Then we created a simple PDF document using HTML string. A certificate file was created from adobe reader which was used to sign the PDF original 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 header 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 watermark. It can be licensed for commercial use and its users can get more information on license from this detailed page.

< PREVIOUS
Java PDF Converter (Code Example Tutorial)
NEXT >
How to Read PDF File in Java

Ready to get started? Version: 2024.6 just released

Free Maven Download View Licenses >
123