How to Verify Digital Signatures in PDF in C#

Andrew Wilson
4 min readAug 28, 2024

--

Digital signatures in PDF files serve as a critical security measure, not only authenticating the source of the document but also guaranteeing that its content remains unaltered throughout the transmission process. Consequently, mastering the correct method to validate a PDF’s digital signature is paramount in safeguarding the document’s integrity and trustworthiness.

This article will focus on how to verify the validity of PDF signatures and verify that whether a PDF document has been modified in C# using a free NET library.

  • Validate Digital Signature in PDF in C#
  • Check if a PDF Has Been Modified in C#

Free .NET Library for Verifying PDF Signatures

Free Spire.PDF for .NET is a free .NET library for processing PDFs (but with certain page limits).

You can install it directly via NuGet, or download the product package via the link below to unzip it and reference the dlls manually.

Validate Digital Signature in PDF in C#

In Free Spire.PDF library, the PdfSignature class represents the digital signatures in PDF. To verify the validity of the signature, we can use the VerifySignature() method of the PdfSignature class.

Main Steps:

  1. Load a PDF file using LoadFromFile() method.
  2. Get the collection of form fields in the PDF file.
  3. Iterate through all the fields and determine whether the current field is a signature field (PdfSignatureFieldWidget class).
  4. If it is, then get the PDF signature through the PdfSignatureFieldWidget.Signature property.
  5. Call the PdfSignature.VerifySignature() method to check the validity of the PDF signature.
  6. Output the result.

Sample C# code:

using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;

namespace ValidatePDFSignature
{
class Program
{
static void Main(string[] args)
{

//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load a PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Get a collection of form fields in the PDF file
PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;
PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;

//Iterate through all fields
for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)
{
//Get the signature fields
if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)
{
PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];

//Get the signatures
PdfSignature signature = signatureFieldWidget.Signature;

//Validate signatures
bool valid = signature.VerifySignature();
if (valid)
{
Console.WriteLine("The signatures are valid.");
}
else
{
Console.WriteLine("The signatures are invalid.");
}
}
}
}
}
}

Result:

C# Validate PDF Signature

Check if a PDF Has Been Modified in C#

Verify whether a PDF document has been modified can also indirectly verify the validity of electronic signatures. If the document content has been modified after being signed, then the PDF signature will become invalid. By using the VerifyDocModified() method of the PdfSignature class, we can verify the integrity of PDF documents quickly.

Main steps:

  1. Create a PdfDocument object and load the PDF file using the LoadFromFile() method
  2. Get the forms in the PDF file, and then get a collection of form fields.
  3. Iterate through all the fields and determine whether the current field is a signature field (PdfSignatureFieldWidget class).
  4. If it is a signature field, then get the PDF signature through the PdfSignatureFieldWidget.Signature property.
  5. Call the PdfSignature.VerifyDocModified() method to verify that if the document has been modified after signing.
  6. Output the result.

Sample C# code:

using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Widget;

namespace GetSignatureCertificate
{
class Program
{
static void Main(string[] args)
{

//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load a PDF document
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

//Get a collection of form fields in the PDF file
PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form;
PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget;

for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++)
{
//Get the signature fields
if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget)
{
PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i];

//Get the signatures
PdfSignature signature = signatureFieldWidget.Signature;

//Check if the document has been modified after signing
bool modified = signature.VerifyDocModified();
if (modified)
{
Console.WriteLine("Document has been modified.");
}
else
{
Console.WriteLine("Document has not been modified.");
}
}
}
}
}
}

Result:

C# Check PDF integrity

With these steps and sample codes, we can ensure the integrity of PDF documents and the validity of electronic signatures, thus providing additional security when dealing with important documents.

Also refer to:

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.