Implementation of adding digital signature to Java pdf

Time:2021-3-4

After the digital signature of the document, people other than the author can not modify it. Therefore, adding digital signature to PDF document can ensure its security and authenticity. At the same time, according to the difference of added content, digital signature can be divided into text digital signature, picture digital signature, picture and text digital signature. This article will pass the Spire.PDF For Java to demonstrate how to add a digital signature to a PDF document.

Tools: Spire.PDF for Java 2.11.4

Jar file acquisition and import:

Method 1: Download jar package from the official website. After downloading, unzip the file and add the Spire.Pdf.jar File into the Java program. (as shown in the figure below)

Method 2: import through Maven warehouse. Please refer to this webpage for the specific installation tutorial.

Example 1: adding a text digital signature

import com.spire.pdf.PdfDocument; 
import com.spire.pdf.graphics.PdfTrueTypeFont; 
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 TextSignature { 
  public static void main(String[] args) { 
//Loading PDF documents 
PdfDocument doc = new PdfDocument(); 
 doc.loadFromFile("C:\Users\Test1\Desktop\Sample.pdf"); 
//Load PFX certificate 
PdfCertificate cert = new PdfCertificate("C:\Users\Test1\Desktop\gary.pfx", "abc123"); 
//Add a digital signature and set its location 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() - 270, (float) doc.getPages().get(0).getActualSize().getHeight() - 300), new Dimension(200, 110)); 
signature.setBounds(rect); 
//Set signature as text 
signature.setGraphicMode(GraphicMode.Sign_Detail); 
//Set the content of the signature 
signature.setNameLabel (the "signer"); 
signature.setName("Gary"); 
signature.setContactInfoLabel ("contact number"); 
signature.setContactInfo("010333555"); 
signature.setDateLabel ("date"); 
signature.setDate(new java.util.Date()); 
signature.setLocationInfoLabel (Location:); 
signature.setLocationInfo("US"); 
signature.setReasonLabel ("reason"); 
signature.setReason ("I am the document owner"); 
signature.setDistinguishedNameLabel("DN: "); 
 signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName()); 
//Set the font for the signature 
signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 11))); 
//Set document permissions to prohibit changes 
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); 
signature.setCertificated(true); 
//Save document 
doc.saveToFile("output/TextSignature.pdf"); 
doc.close(); 
  } 
}

Text digital signature effect:

Example 2: add picture digital signature

import com.spire.pdf.PdfDocument; 
import com.spire.pdf.graphics.PdfImage; 
import com.spire.pdf.graphics.PdfTrueTypeFont; 
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 ImageSignature { 
  public static void main(String[] args) { 
//Loading PDF documents 
PdfDocument doc = new PdfDocument(); 
  doc.loadFromFile("C:\Users\Test1\Desktop\Sample.pdf"); 
//Load PFX certificate 
PdfCertificate cert = new PdfCertificate("C:\Users\Test1\Desktop\gary.pfx", "abc123"); 
 
//Add a digital signature and set its location 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() - 250, (float) doc.getPages().get(0).getActualSize().getHeight() - 300), new Dimension(170, 150)); 
signature.setBounds(rect); 
//Set signature as picture 
signature.setGraphicMode(GraphicMode.Sign_Image_Only); 
  signature.setSignImageSource(PdfImage.fromFile("C:\Users\Test1\Desktop\Image.png")); 
//Set the font for the signature 
signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 11))); 
//Set document permissions to prohibit changes 
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); 
signature.setCertificated(true); 
//Save document 
doc.saveToFile("output/ImageSignature.pdf"); 
doc.close(); 
  } 
}

Image digital signature effect:

Example 3: add digital signature of picture and text

import com.spire.pdf.PdfDocument; 
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 TextAndImageSignature { 
  public static void main(String[] args) { 
//Loading PDF documents 
PdfDocument doc = new PdfDocument(); 
    doc.loadFromFile("C:\Users\Test1\Desktop\Sample.pdf"); 
//Load PFX certificate 
PdfCertificate cert = new PdfCertificate("C:\Users\Test1\Desktop\gary.pfx", "abc123"); 
//Add a digital signature and set its location 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() - 350, (float) doc.getPages().get(0).getActualSize().getHeight() - 170), new Dimension(300, 170)); 
signature.setBounds(rect); 
//Set signature to picture plus text mode 
signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail); 
//Set the content of the signature 
signature.setNameLabel (the "signer"); 
signature.setName("Gary"); 
signature.setContactInfoLabel ("contact number"); 
signature.setContactInfo("010333555"); 
signature.setDateLabel ("date"); 
signature.setDate(new java.util.Date()); 
signature.setLocationInfoLabel (Location:); 
signature.setLocationInfo("US"); 
signature.setReasonLabel ("reason"); 
signature.setReason ("I am the document owner"); 
signature.setDistinguishedNameLabel("DN: "); 
   signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName()); 
  signature.setSignImageSource(PdfImage.fromFile("C:\Users\Test1\Desktop\Image.png")); 
//Set the font for the signature signature.setSignDetailsFont (new PdfTrueTypeFont(new Font("Arial Unicode MS",  Font.PLAIN , 11))); 
//Set document permissions to prohibit changes 
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); 
signature.setCertificated(true); 
//Save document 
doc.saveToFile("output/TextAndImageSignature.pdf"); 
doc.close(); 
 
  } 
}

Image and text digital signature effect:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.