Verifying digital signatures in PDFs is crucial for ensuring that a document remains unaltered and genuinely comes from the stated signer. This verification process is essential for maintaining the document’s integrity and trustworthiness. Additionally, extracting digital signatures allows you to retrieve signature details, such as the signature image and certificate information, which can be useful for further validation or archival purposes. In this article, we will demonstrate how to verify and extract digital signatures in PDFs in Java using Spire.PDF for Java.
- Verify Digital Signatures in PDF in Java
- Detect Whether a Signed PDF Has Been Modified in Java
- Extract Signature Images and Certificate Information from PDF in Java
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.10.7</version> </dependency> </dependencies>
Verify Digital Signatures in PDF in Java
Spire.PDF for Java provides the PdfSignature.verifySignature() method to check the validity of digital signatures in PDF documents. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Get the form of the PDF document using the PdfDocument.Form property.
- Iterate through all fields in the form and find the signature field.
- Get the signature using the PdfSignatureFieldWidget.getSignature() method.
- Verify the validity of the signature using the PdfSignature.verifySignature() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.fields.PdfField; import com.spire.pdf.security.PdfSignature; import com.spire.pdf.widget.PdfFormWidget; import com.spire.pdf.widget.PdfSignatureFieldWidget; public class VerifySignature { public static void main(String[] args) { // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a PDF document pdf.loadFromFile("Signature.pdf"); // Get the form of the PDF document PdfFormWidget formWidget = (PdfFormWidget) pdf.getForm(); if(formWidget.getFieldsWidget().getCount() > 0) { // Iterate through all fields in the form for(int i = 0; i < formWidget.getFieldsWidget().getCount(); i ++) { PdfField field = formWidget.getFieldsWidget().get(i); // Find the signature field if (field instanceof PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureField = (PdfSignatureFieldWidget) field; // Get the signature PdfSignature signature = signatureField.getSignature(); // Verify the signature boolean valid = signature.verifySignature(); if(valid) { System.out.print("The signature is valid!"); } else { System.out.print("The signature is invalid!"); } } } } } }
Detect Whether a Signed PDF Has Been Modified in Java
To verify if a signed PDF document has been modified, you can use the PdfSignature.VerifyDocModified() method. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Get the form of the PDF document using the PdfDocument.Form property.
- Iterate through all fields in the form and find the signature field.
- Get the signature using the PdfSignatureFieldWidget.getSignature() method.
- Verify if the document has been modified since it was signed using the PdfSignature.VerifyDocModified() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.fields.PdfField; import com.spire.pdf.security.PdfSignature; import com.spire.pdf.widget.PdfFormWidget; import com.spire.pdf.widget.PdfSignatureFieldWidget; public class CheckIfSignedPdfIsModified { public static void main(String[] args) { // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a PDF document pdf.loadFromFile("Signature.pdf"); // Get the form of the PDF document PdfFormWidget formWidget = (PdfFormWidget) pdf.getForm(); if(formWidget.getFieldsWidget().getCount() > 0) { // Iterate through all fields in the form for (int i = 0; i < formWidget.getFieldsWidget().getCount(); i++) { PdfField field = formWidget.getFieldsWidget().get(i); // Find the signature field if (field instanceof PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureField = (PdfSignatureFieldWidget) field; // Get the signature PdfSignature signature = signatureField.getSignature(); // Verify the signaure boolean modified = signature.verifyDocModified(); if(modified) { System.out.print("The document has been modified!"); } else { System.out.print("The document has not been modified!"); } } } } } }
Extract Signature Images and Certificate Information from PDF in Java
To extract signature images and certificate information from PDF, you can use the PdfFormWidget.extractSignatureAsImages() and PdfSignture.getCertificate().toString() methods. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Get the form of the PDF document using the PdfDocument.Form property.
- Extract signature images using the PdfFormWidget.extractSignatureAsImages() method and then save each image to file.
- Iterate through all fields in the form and find the signature field.
- Get the signature using the PdfSignatureFieldWidget.getSignature() method.
- Get the certificate information of the signature using the PdfSignture.getCertificate().toString() method.
- Java
import com.spire.pdf.PdfDocument; import com.spire.pdf.fields.PdfField; import com.spire.pdf.security.PdfCertificate; import com.spire.pdf.security.PdfSignature; import com.spire.pdf.widget.PdfFormWidget; import com.spire.pdf.widget.PdfSignatureFieldWidget; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class ExtractSignatureImage { public static void main(String[] args) { // Create a PdfDocument object PdfDocument pdf = new PdfDocument(); // Load a PDF document pdf.loadFromFile("Signature.pdf"); // Get the form of the PDF document PdfFormWidget formWidget = (PdfFormWidget) pdf.getForm(); // Extract signature images Image[] images = formWidget.extractSignatureAsImages(); // Iterate through the images and save each image to file for (int i = 0; i < images.length; i++) { try { // Convert the Image to BufferedImage BufferedImage bufferedImage = (BufferedImage) images[i]; // Define the output file path File outputFile = new File("output\\signature_" + i + ".png"); // Save the image as a PNG file ImageIO.write(bufferedImage, "png", outputFile); } catch (IOException e) { e.printStackTrace(); } } // Create a text file to save the certificate information try (BufferedWriter writer = new BufferedWriter(new FileWriter("output\\certificate_info.txt"))) { if (formWidget.getFieldsWidget().getCount() > 0) { // Iterate through all fields in the form for (int i = 0; i < formWidget.getFieldsWidget().getCount(); i++) { PdfField field = formWidget.getFieldsWidget().get(i); // Find the signature field if (field instanceof PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureField = (PdfSignatureFieldWidget) field; // Get the signature PdfSignature signature = signatureField.getSignature(); // Get the certificate info of the signature String certificateInfo = signature.getCertificate() != null ? signature.getCertificate().toString() : "No certificate"; // Write the certificate information to the text file writer.write("Certificate Info: \n" + certificateInfo); writer.write("-----------------------------------\n"); } } } else { writer.write("No signature fields found."); } } catch (IOException e) { e.printStackTrace(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.