DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add and Verify Digital Signature in PDF with Java

Introduction

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.

Prerequisites

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

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>2.4.4</version>
    </dependency>
</dependencies>

(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 com.spire.pdf.security.GraphicMode;
import com.spire.pdf.security.PdfCertificate;
import com.spire.pdf.security.PdfCertificationFlags;
import com.spire.pdf.security.PdfSignature;
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();
        doc.loadFromFile("Input.pdf");
        //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.setName("Gary");
        signature.setContactInfoLabel("ContactInfo:  ");
        signature.setContactInfo("010-123456");
        signature.setDateLabel("Date:  ");
        signature.setDate(new Date());
        signature.setLocationInfoLabel("Location:  ");
        signature.setLocationInfo("America");
        signature.setReasonLabel("Reason:  ");
        signature.setReason("The certificate of this document.");
        signature.setDistinguishedNameLabel("DL:  ");
        signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
        signature.setGraphicMode(GraphicMode.Sign_Name_And_Sign_Detail);
        signature.setDocumentPermissions(PdfCertificationFlags.Allow_Form_Fill);
        signature.setCertificated(true);

        //Save the file
        doc.saveToFile("AddSignature.pdf");
    }
}

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.security.PdfSignature;
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();
        doc.loadFromFile("AddSignature.pdf");

        //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)