Add a Digital Signature to a PDF - Console C#

This tutorial shows how to add a digital signature certificate to a PDF file and read back to the signature information in a C# Window Console application using the LEADTOOLS SDK. A certificate-based digital signature adds security to the document by guaranteeing that the document has not been modified.

Overview  
Summary This tutorial covers how to digitally sign a document in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (5 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET Framework (Console)
  • Java: Java

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Add a Digital Signature to a PDF - Console C# tutorial.

Digital Signature

Digital signatures provide document security by using a PFX encryption file to authenticate the signer's identity.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Sign Document Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Pdf; 

Inside the Main() method, add 3 new string variables named inputFile, outputFile, and signatureFile.

C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   string inputFile = @"SOURCE PDF FILE PATH"; 
   string outputFile = @"FILE PATH TO OUTPUT SIGNED PDF TO"; 
   string signatureFile = @"FILE PATH TO PFX SIGNATURE FILE"; 
 
   if (CheckDigitalSignatureSupportStatus() == false) 
   { 
      Console.WriteLine("Digital Signature functionality is not available.\n" + 
         "Make sure you have the Open SSL libs:\n" + 
         "https://www.leadtools.com/help/sdk/v22/dh/to/lead-compiled-openssl-binaries.html"); 
      return; 
   } 
 
   SignPDFDocument(inputFile, outputFile, signatureFile, "password"); 
   ParsePDF(outputFile); 
} 

Inside the Program class, add a new method named CheckDigitalSignatureSupportStatus(). Call this method inside the Main() method in a conditional statement, as shown above. Add the code below to the new method to set the directory to the OpenSSL binaries and check the digital signature support status.

C#
public static bool CheckDigitalSignatureSupportStatus() 
{ 
   RasterDefaults.SetResourceDirectory(LEADResourceDirectory.OpenSSL, @"FILE PATH TO OpenSSL BINARIES DIRECTORY(%LEADTOOLS22_SETUP%\Bin\Dotnet4\x64)"); 
 
   return PDFDocument.GetDigitalSignatureSupportStatus() == RasterExceptionCode.Success; 
} 

Note

If you do not have the LEAD-Compiled OpenSSL binaries, you can download them here.

Next, add two more methods to the Program class named SignPDFDocument(string inputPdf, string outputPdf, string signatureFile, string password = null) and ParsePDF(string filename). Call both of these methods inside the Main() method, as shown above.

Add the below code to the SignPDFDocument() to create a new PDFFile, digitally sign the PDF, and export it to the file path specified.

C#
static void SignPDFDocument(string inputPdf, string outputPdf, string signatureFile, string password = null) 
{ 
   try 
   { 
      PDFFile inputDoc = new PDFFile(inputPdf); 
      inputDoc.SignDocument(outputPdf, signatureFile, password); 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.Message); 
      return; 
   } 
   Console.WriteLine("Signed PDF successfully"); 
} 

Add the code below to the ParsePDF() method to parse the signature inside the PDF, if there is one.

C#
static void ParsePDF(string filename) 
{ 
   PDFDocument doc = new PDFDocument(filename); 
   doc.ParsePages(PDFParsePagesOptions.Signatures, 1, -1); 
   Console.WriteLine(); // Act as a spacer 
 
   foreach(PDFDocumentPage page in doc.Pages) 
   { 
      Console.WriteLine($"Page {page.PageNumber}\n"); 
      if (page.Signatures.Count == 0) 
         Console.WriteLine("No signatures on this page\n"); 
 
      foreach (PDFSignature sig in page.Signatures){ 
         PrintSignatureValues(sig); 
      } 
   } 
} 

Add a new method to the Program class named PrintSignatureValues(PDFSignature signature). Call this method inside the ParsePDF() method, as shown above. Add the code below to the PrintSignatureValues() method to print out the extracted information from the PDFSignature class, including the issuer, public key, serial number, subject, valid start and end dates, and the version of the signature.

C#
private static void PrintSignatureValues(PDFSignature signature) 
{ 
   Console.WriteLine($"Issuer: {signature.CertificateInfo[PDFSignature.CertificateInfoIssuer]}"); 
   Console.WriteLine($"Public Key: {signature.CertificateInfo[PDFSignature.CertificateInfoPublicKey]}"); 
   Console.WriteLine($"Serial Number: {signature.CertificateInfo[PDFSignature.CertificateInfoSerialNumber]}"); 
   Console.WriteLine($"Subject: {signature.CertificateInfo[PDFSignature.CertificateInfoSubject]}"); 
   Console.WriteLine($"Valid start date: {signature.ValidFrom.ToLocalTime()}"); 
   Console.WriteLine($"Valid end date: {signature.ValidTo.ToLocalTIme()}"); 
   Console.WriteLine($"Version: {signature.Version}"); 
 
   Console.WriteLine(); // Act as a spacer 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs, signs the given PDF with the given digital signature, and then prints out the signature details to the console.

Wrap-Up

This tutorial showed how to sign a PDF using a digital signature certificate file, and showed how to parse values from a document's digital signature. It also covered how to use the PDFFile, PDFDocument, PDFSignature, and PDFDocumentPage classes.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.