Skip to main content
All docs
V23.2

How to: Validate a PDF Document Signature

  • 3 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

The PDF Document API allows you to validate PKCS #7 signatures in a PDF document. Use the PdfPkcs7Signature class methods to obtain information about the PKCS#7 signature and verify it.

The example below shows how to create a console app that obtains the PKCS#7 signatures from a PDF document (Document.pdf in this example). Once the signature is obtained, the code checks attributes such as the time of signing, the signer’s identity, and authenticity of the signature. The result is shown in a console.

using DevExpress.Pdf;
// ...
// Get the Document.pdf file.
using (PdfDocumentSigner documentSigner = new PdfDocumentSigner("Document.pdf"))
    // Retrieve the list of `PdfSignatureInfo` objects in the document.
    foreach (var signature in documentSigner.GetSignatureInfo()) {
        Console.WriteLine("Signature field name : {0}", signature.FieldName);
        Console.WriteLine("  Signer name : {0}", signature.SignerName);
        // Obtain the PKCS#7 signature from the list of `PdfSignatureInfo` objects and verify it.
        var pkcs7 = documentSigner.GetPdfPkcs7Signature(signature.FieldName);
        Console.WriteLine("  Is signature valid? : {0}", pkcs7.VerifySignature());
        // Obtain the PKCS#7 signature certificate and verify the certificate info.
        var certificate = pkcs7.GetSignatureCertificate();
        Console.WriteLine("  Certificate issuer : {0}", certificate.IssuerName.Name);
        Console.WriteLine("  Is certificate valid? : {0}", certificate.Verify());
        var timestampDate = pkcs7.GetTimeStampDate();
        // Get a timestamp and verify the time of signing.
        Console.WriteLine("  Does signature include a timestamp? : {0}", timestampDate.HasValue);
        if(timestampDate.HasValue) {
            Console.WriteLine("  Timestamp : {0}", timestampDate);
            Console.WriteLine("  Is timestamp valid? : {0}", pkcs7.VerifyTimeStamp());
        }
    }

The result is shown below:

Signature field name : Signature1
  Signer name : DevExpress Demo
  Is signature valid? : True
  Certificate issuer : CN=DevExpress Demo
  Is certificate valid? : False
  Does signature include a timestamp? : False
Signature field name : 9a1677be-7fa2-4429-8cb3-bea0c737ebf4
  Signer name : DevExpress Demo
  Is signature valid? : True
  Certificate issuer : CN=DevExpress Demo
  Is certificate valid? : False
  Does signature include a timestamp? : True
  Timestamp : 11/14/2023 8:02:57 AM
  Is timestamp valid? : True