DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

Add Digital Signatures to PDF in Java

Digital signature ensures that the signed document wasn’t changed by anyone other than its author. It is the most common way to assure the authenticity of the document content. In this article, I’ll show you how to digitally sign a PDF document using Spire.PDF for Java.

Before you can run the following examples, you’re required to add Spire.Pdf.jar as dependency in your project.

Example 1. Add text signature to PDF

//Load a pdf document 
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

//Load the certificate 
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\gary.pfx", "e-iceblue");

//Create a PdfSignature object and specify its position and size 
PdfSignature signature = new PdfSignature(doc, doc.getPages().get(doc.getPages().getCount()-1), cert, "MySignature");
Rectangle2D rect = new Rectangle2D.Float();
rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 340, (float) doc.getPages().get(0).getActualSize().getHeight() - 150), new Dimension(290, 110));
signature.setBounds(rect);

//Set the graphics mode 
signature.setGraphicMode(GraphicMode.Sign_Detail);

//Set the signature content 
signature.setNameLabel("Singer:");
signature.setName("Gary");
signature.setContactInfoLabel("Telephone:");
signature.setContactInfo("010333555");
signature.setDateLabel("Date:");
signature.setDate(new java.util.Date());
signature.setLocationInfoLabel("Location:");
signature.setLocationInfo("USA");
signature.setReasonLabel("Reason:");
signature.setReason("I am the author");
signature.setDistinguishedNameLabel("DN: ");
signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());

//Set the signature font 
signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 11)));

//Set the document permission 
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);

//Save to file 
doc.saveToFile("output/TextSignature.pdf");
doc.close();

Alt Text

Example 2. Add image signature to PDF

//Load a pdf document 
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

//Load the certificate 
PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\gary.pfx", "e-iceblue");

//Create a PdfSignature object and specify its position and size 
PdfSignature signature = new PdfSignature(doc, doc.getPages().get(doc.getPages().getCount()-1), cert, "MySignature");
Rectangle2D rect = new Rectangle2D.Float();
rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 230, (float) doc.getPages().get(0).getActualSize().getHeight() - 200), new Dimension(150, 150));
signature.setBounds(rect);

//Set the graphics mode 
signature.setGraphicMode(GraphicMode.Sign_Image_Only);
signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\log-verified.png"));

//Set the signature font 
signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 11)));

//Set the document permission 
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);
//Save to file 
doc.saveToFile("output/ImageSignature.pdf");
doc.close();

Alt Text

Example 3. Add text and image signature to PDF

import com.spire.pdf.graphics.*;
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.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class AddDigitalSignature {

    public static void main(String[] args) {

        //Load a pdf document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

        //Load the certificate file
        PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\gary.pfx", "e-iceblue");

        //Create a PdfSignature object and specify its position and size
        PdfSignature signature = new PdfSignature(doc, doc.getPages().get(doc.getPages().getCount()-1), cert, "MySignature");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 300, (float) doc.getPages().get(0).getActualSize().getHeight() - 170), new Dimension(250, 120));
        signature.setBounds(rect);

        //Set the graphics mode
        signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

        //Set the signature content
        signature.setNameLabel("Singer:");
        signature.setName("Gary");
        signature.setContactInfoLabel("Telephone:");
        signature.setContactInfo("010333555");
        signature.setDateLabel("Date:");
        signature.setDate(new java.util.Date());
        signature.setLocationInfoLabel("Location:");
        signature.setLocationInfo("USA");
        signature.setReasonLabel("Reason:");
        signature.setReason("I am the author");
        signature.setDistinguishedNameLabel("DN: ");
        signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
        signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\sig-name.png"));

        //Set the signature font
        signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 11)));

        //Set the document permission
        signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
        signature.setCertificated(true);

        //Save to file
        doc.saveToFile("output/TextAndImageSignature.pdf");
        doc.close();
    }
}

Alt Text

Top comments (1)

Collapse
 
eoscorp profile image
eosCorp

Great content