DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add and Verify Digital Signature in PDF with Java


Digital signature is an effective and commonly used way to protect PDF files from changing by others without permission. This article is going to illustrate how to add digital signature into a PDF file and verify the digital signature using a free Java PDF API - Free Spire.PDF for Java.


For maven projects, we could easily add dependencies to the Free Spire.PDF for Java API by adding the following code in pom.xml:


(The latest version of Free Spire.PDF for Java in maven repository can be found here)

For common Java projects, download the latest version of Free Spire.PDF for Java (download link), then add Spire.Pdf.jar from the lib folder into your project.

Using the code

. Add digital signature into a PDF file

To create digital signature, we need to create an object of the PdfSignature class and then set signature properties. The below code example shows how to add digital signature to a PDF page at a specified location.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.geom.Rectangle2D;
import java.util.Date;

public class AddDigitalSignature {
    public static void main(String[] args) {

        //Load the PDF file
        PdfDocument doc = new PdfDocument();
        //Get the first Page
        PdfPageBase page = doc.getPages().get(0);

        Rectangle2D.Float rec = new Rectangle2D.Float(100, 450, 310, 100);

        //Load the .pfx certificate file
        PdfCertificate certificate = new PdfCertificate("gary.pfx", "e-iceblue");

        //Add digital signature to the first page at specified location
        PdfSignature signature = new PdfSignature(doc, page, certificate, "signature1", rec);

        //Set signature details
        signature.setNameLabel("Signer:  ");
        signature.setContactInfoLabel("ContactInfo:  ");
        signature.setDateLabel("Date:  ");
        signature.setDate(new Date());
        signature.setLocationInfoLabel("Location:  ");
        signature.setReasonLabel("Reason:  ");
        signature.setReason("The certificate of this document.");
        signature.setDistinguishedNameLabel("DL:  ");

        //Save the file

Add Signature

. Verify digital signature

The verifySignature() method in PdfSignature class is used to verify if a digital signature is valid or not.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.widget.PdfFormFieldWidgetCollection;
import com.spire.pdf.widget.PdfFormWidget;
import com.spire.pdf.widget.PdfSignatureFieldWidget;

public class VerifyDigitalSignature {
    public static void main(String[] args) {

        //Load the PDF file
        PdfDocument doc = new PdfDocument();

        //Get the forms collection
        PdfFormWidget pdfFormWidget = (PdfFormWidget) doc.getForm();
        PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.getFieldsWidget();

        //Get the signature field
        for (int i = 0; i < pdfFormFieldWidgetCollection.getCount(); i++) {
            if (pdfFormFieldWidgetCollection.get(i) instanceof PdfSignatureFieldWidget) {
                PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget) pdfFormFieldWidgetCollection.get(i);
                //Get the signature in the signature field
                PdfSignature signature = signatureFieldWidget.getSignature();
                //Verify the signature
                boolean result = signature.verifySignature();
                if(result) {
                    System.out.println("Signature is valid");
                }else {
                    System.out.println("Signature is invalid");

Verify Signature

Top comments (0)