Adding Signatures to PDFs
Java developers can add electronic and digital signatures to PDF documents programmatically using IronPDF for Java's Signature
and SignatureManager
classes.
For digital signatures, instantiate a new Signature
class with the location of a valid .pfx
or .p12
certificate, along with its password. Next, call the signPdfWithSignature
method on the working PDF's SignatureManager
to sign the PDF with the certificate.
IronPDF also supports the inclusion of signature images, in the form of computer-generated text images or digitized images of handwriting. Add such images to a signature with the addSignatureImage
method prior to signing the PDF.
Need to add a handwritten signature on a PDF page instead? Use the HtmlStamper
class to add HTML text and images to one or more pages of a document.
How to Add a Digital Signature to a PDF Programmatically in Java
- Install the IronPDF Java library to sign a digital signature on a PDF.
- Use intuitive APIs to load existing PDFs or render new PDFs.
- Create a
Signature
object and customize its granular information. - Access
SignatureManager
by calling thegetSignature
method on the PDF instance. - Sign the PDF with the
signPdfWithSignature
method in theSignatureManager
instance.
Here's a sample code implementation for signing a PDF with a digital signature using IronPDF:
import com.ironpdf.PdfDocument;
import com.ironpdf.security.Signature;
import com.ironpdf.security.SignatureManager;
public class PdfSignatureExample {
public static void main(String[] args) {
try {
// Load the PDF document
PdfDocument pdf = new PdfDocument("input.pdf");
// Load the digital certificate
Signature signature = new Signature("certificate.pfx", "certificatePassword");
// Optional: Add an image to the signature (e.g., a scanned signature)
signature.addSignatureImage("signatureImage.png");
// Get the SignatureManager instance from the PDF
SignatureManager signatureManager = pdf.getSignatureManager();
// Sign the PDF with the digital signature
signatureManager.signPdfWithSignature(signature, "output_signed.pdf");
System.out.println("PDF signed successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import com.ironpdf.PdfDocument;
import com.ironpdf.security.Signature;
import com.ironpdf.security.SignatureManager;
public class PdfSignatureExample {
public static void main(String[] args) {
try {
// Load the PDF document
PdfDocument pdf = new PdfDocument("input.pdf");
// Load the digital certificate
Signature signature = new Signature("certificate.pfx", "certificatePassword");
// Optional: Add an image to the signature (e.g., a scanned signature)
signature.addSignatureImage("signatureImage.png");
// Get the SignatureManager instance from the PDF
SignatureManager signatureManager = pdf.getSignatureManager();
// Sign the PDF with the digital signature
signatureManager.signPdfWithSignature(signature, "output_signed.pdf");
System.out.println("PDF signed successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation:
- PdfDocument: Used to load an existing PDF document.
- Signature: Represents the digital signature. It's initialized with a certificate file and its password.
- addSignatureImage: Adds an optional image to the signature, such as a scanned signature.
- SignatureManager: Accessed via
getSignatureManager()
, responsible for managing the signing process. - signPdfWithSignature: Signs the PDF with the provided signature and outputs to a specified file.
This code sample outlines how to use the IronPDF library to apply a digital signature to a PDF programmatically. Make sure to replace "input.pdf"
, "certificate.pfx"
, "certificatePassword"
, and "signatureImage.png"
with your actual file paths and credentials.