This tutorial shows how to read and write a digital signature certificate to a PDF file in a C# .NET 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# .NET Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.
Digital signatures provide document security by using a PFX encryption file to authenticate the signer's identity.
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:
Leadtools.Pdf
If using local DLL references, the following DLLs are needed.
The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Core.dll
Leadtools.Pdf.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.
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:
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
:
using Leadtools;
using Leadtools.Pdf;
Inside the Main()
method, add 3 new string variables named inputFile
, outputFile
, and signatureFile
.
static void Main(string[] args)
{
InitLEAD();
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/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.
public static bool CheckDigitalSignatureSupportStatus()
{
RasterDefaults.SetResourceDirectory(LEADResourceDirectory.OpenSSL, @"FILE PATH TO OpenSSL BINARIES DIRECTORY(%LEADTOOLS23_SETUP%\Bin\net)");
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.
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.
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.
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 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.
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.