Validate PDF Signatures in C# and .NET

Digital signatures can be applied for many types of documents where traditional pen-and-ink signatures were used in the past. However, the mere existence of a digital signature is not adequate assurance that a document is what it appears to be. Moreover, government and enterprise settings often need to impose additional constraints on their signature workflows, such as restricting user choices and document behavior during and after signing.

For these reasons, the PDF provides mechanisms for two broad categories of tasks:

  • Fully trusting an electronic document by enabling verification that the signed document has not been altered and that it was signed by someone the recipient trusts.
  • Creating and controlling feature-rich and secure digital signature workflows.

In a PDF, signature information is contained in a signature dictionary. Objects in the dictionary are defined by the PDF Reference.

The signature dictionary can reference, or be referenced by, other dictionaries. The entries in these dictionaries determine the nature and features of the signature, and by extension, what data can be available to any PDF viewer designed to process the signature data.

While other viewers may vary in their support of PDF language features, PDF .Net by SautinSoft supports all of those features. At a high level, that process can be grouped into these categories:

  • Adding a digital signature to a document.
  • Checking that signature for validity.
  • Permissions and restrictions that control the signature workflow.

Digital signatures on PDF documents can tell you if the PDF has been modified. You can use the PDF. Net library to verify digital signatures on PDF files. The library can help you read signature properties in .NET Framework and .NET Core applications.

General PDF digital signature authenticity special review:

  • No changes have been made to the document since it was signed.
  • Who signed it is true.
How it works in PDF. Net. We have a special class - (PdfSignature Class)

And after loading your PDF document inside, the engine is checking the field - signatureValidationResult.IsValid:


                Var signature = signatureField.Value;

                    if (signature != null)
                    {
                        var signatureValidationResult = signature.Validate();

                        if (signatureValidationResult.IsValid)
                        {
                            Console.Write("Signature '{0}' is VALID, signed by '{1}'. ", signatureField.Name, signature.Content.SignerCertificate.SubjectCommonName);
                            Console.WriteLine("The document has not been modified since this signature was applied.");
                        }
                        else
                        {
                            Console.Write("Signature '{0}' is INVALID. ", signatureField.Name);
                            Console.WriteLine("The document has been altered or corrupted since the signature was applied.");
                        }

To verify digital signature in PDF C# code is presented here that uses the PdfSignature class object to accomplish the task. The result of validation will be like this:
  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF document.
  3. Checking the PDF signature field.
  4. Determining the number of signatures.
  5. Checking for validation.

Complete code

using SautinSoft.Pdf;
using SautinSoft.Pdf.Forms;
using System;

class Program
{
    static void Main()
    {
       
        using (var document = PdfDocument.Load(@"..\..\Multiple Digital Signature.pdf"))
        {
            foreach (var field in document.Form.Fields)
                if (field.FieldType == PdfFieldType.Signature)
                {
                    var signatureField = (PdfSignatureField)field;

                    var signature = signatureField.Value;

                    if (signature != null)
                    {
                        var signatureValidationResult = signature.Validate();

                        if (signatureValidationResult.IsValid)
                        {
                            Console.Write("Signature '{0}' is VALID, signed by '{1}'. ", signatureField.Name, signature.Content.SignerCertificate.SubjectCommonName);
                            Console.WriteLine("The document has not been modified since this signature was applied.");
                        }
                        else
                        {
                            Console.Write("Signature '{0}' is INVALID. ", signatureField.Name);
                            Console.WriteLine("The document has been altered or corrupted since the signature was applied.");
                        }
                    }
                }
        }
    }
}

Download

Option Infer On

Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Forms
Imports System

Friend Class Program
	Shared Sub Main()

		Using document = PdfDocument.Load("..\..\Multiple Digital Signature.pdf")
			For Each field In document.Form.Fields
				If field.FieldType = PdfFieldType.Signature Then
					Dim signatureField = CType(field, PdfSignatureField)

					Dim signature = signatureField.Value

					If signature IsNot Nothing Then
						Dim signatureValidationResult = signature.Validate()

						If signatureValidationResult.IsValid Then
							Console.Write("Signature '{0}' is VALID, signed by '{1}'. ", signatureField.Name, signature.Content.SignerCertificate.SubjectCommonName)
							Console.WriteLine("The document has not been modified since this signature was applied.")
						Else
							Console.Write("Signature '{0}' is INVALID. ", signatureField.Name)
							Console.WriteLine("The document has been altered or corrupted since the signature was applied.")
						End If
					End If
				End If
			Next field
		End Using
	End Sub
End Class

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.