Verify digital signature in PDF in Java

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.widget.*;

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

        //Load a pdf document
        PdfDocument doc = new PdfDocument();

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