I'm a new user for Spire.pdf and I would like to validate a test PDF signature through the below code:
- Code: Select all
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("/Users/fernando/Desktop/Test_pdf_signed.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");
}
}
}
}
}
I can see the signature info from adobe ("Signer's identity is valid" ), but I don't know why the Sprie code always prints the "Invalid signature",
I was using 5.1.0 which can not work as expected, after I upgraded to 8.8.3 the same result happened.
Could you pls give me some guidance or suggestions? Thanks!
ps: I have sent the test PDF to your support email [email protected] and linked this page.