Verify digital signature in PDF in Java

Tuesday, 25 August 2020 09:15 Written by  support iceblue
Rate this item
(0 votes)

This article will demonstrate how to use Spire.PDF for Java to verify the digital signature in PDF in Java applications.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfSignature;
import com.spire.pdf.widget.*;

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

        //Load a pdf document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("Sample.pdf");

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

        //Traverse all the PDF form field
        for (int i = 0; i < pdfFormFieldWidgetCollection.getCount(); i++) {
            //check whether it is PdfSignatureField
            if (pdfFormFieldWidgetCollection.get(i) instanceof PdfSignatureFieldWidget) {
                //get the signature field
                PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget) pdfFormFieldWidgetCollection.get(i);
                //get the PDF signature
                PdfSignature signature = signatureFieldWidget.getSignature();

                //Verify the signature
                boolean result = signature.verifySignature();
                if (result) {
                    System.out.println("Valid signature");
                } else {
                    System.out.println("Invalid signature");
                }
            }
        }
    }
}

After run the project, we will get the verify signature results:

Verify digital signature in PDF in Java

Additional Info

  • tutorial_title:
Last modified on Thursday, 02 September 2021 06:35