Signature Properties Extraction in PDF with C# and .NET

In the digital world, ensuring the authenticity and integrity of documents is paramount. Digital signatures serve this purpose by providing a secure and verifiable means of signing documents. In C# and .NET, developers can easily read digital signature properties from PDF documents, which can be crucial for validation and auditing purposes.

This article will guide you through the process of accessing digital signature information in using Sautinsoft.PDF .Net library.

To read digital signature properties from a PDF document, follow these steps:

  1. Add SautinSoft.PDF from NuGet.
  2. Load a PDF document.
  3. Find the digital signature field.
  4. Read the digital signature' content: name, date, location, reason, contact information.
  5. Read the contents of the certificate.

Input file:

Output result:

Complete code

using System;
using System.IO;
using Org.BouncyCastle.X509;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using SautinSoft.Pdf.Forms;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// Read digital signature properties from a PDF document using C# and .NET
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/read-digital-signature-properties-from-a-pdf-document-using-csharp-dotnet.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            string inpFile = Path.GetFullPath(@"..\..\..\digital signature.pdf");            

            using (PdfDocument document = PdfDocument.Load(inpFile))
            {
                var field = document.Form.Fields.FirstOrDefault(f => f is PdfSignatureField);
                if (field != null && field is PdfSignatureField signField)
                {
                    Console.WriteLine($"Name: {signField.Value.Name}.");
                    Console.WriteLine($"Date: {signField.Value.Date}.");
                    Console.WriteLine($"Location: {signField.Value.Location}.");
                    Console.WriteLine($"Reason: {signField.Value.Reason}.");
                    Console.WriteLine($"ContactInfo: {signField.Value.ContactInfo}.");

                    var content = signField.Value.Content;
                    X509Certificate certificate = new X509Certificate(content.SignerCertificate.GetRawData());
                    Console.WriteLine("SignerCertificate data:");
                    Console.WriteLine(certificate);
                }
                else
                {
                    Console.WriteLine("The PDF doesn't have a digital signature!");
                }
            }
        }
    }
}

Download

Option Infer On

Imports System
Imports System.IO
Imports Org.BouncyCastle.X509
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Imports SautinSoft.Pdf.Forms

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' Read digital signature properties from a PDF document using C# and .NET
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/read-digital-signature-properties-from-a-pdf-document-using-csharp-dotnet.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free 100-day trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			Dim inpFile As String = Path.GetFullPath("..\..\..\digital signature.pdf")

			Using document As PdfDocument = PdfDocument.Load(inpFile)
				Dim field = document.Form.Fields.FirstOrDefault(Function(f) TypeOf f Is PdfSignatureField)
				Dim tempVar As Boolean = TypeOf field Is PdfSignatureField
				Dim signField As PdfSignatureField = If(tempVar, CType(field, PdfSignatureField), Nothing)
				If field IsNot Nothing AndAlso tempVar Then
					Console.WriteLine($"Name: {signField.Value.Name}.")
					Console.WriteLine($"Date: {signField.Value.Date}.")
					Console.WriteLine($"Location: {signField.Value.Location}.")
					Console.WriteLine($"Reason: {signField.Value.Reason}.")
					Console.WriteLine($"ContactInfo: {signField.Value.ContactInfo}.")

					Dim content = signField.Value.Content
					Dim certificate As New X509Certificate(content.SignerCertificate.GetRawData())
					Console.WriteLine("SignerCertificate data:")
					Console.WriteLine(certificate)
				Else
					Console.WriteLine("The PDF doesn't have a digital signature!")
				End If
			End Using
		End Sub
	End Class
End Namespace

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.