Add a Digital Signature to a PDF - C# .NET Framework

This tutorial shows how to add adigital signaturecertificate to a PDF file and read back to the signature information in a C# Windows 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 2019, 2022
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 theAdd References and Set a Licensetutorial, before working on theAdd a Digital Signature to a PDF - C# .NET Frameworktutorial.

Digital Signature

Digital signaturesprovide 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 theAdd References and Set a Licensetutorial. 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\LEADTOOLS22\Bin\Dotnet4\x64:

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

Set the License File

公关所需许可解锁的功能oject. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer toSetting a Runtime License.

There are two types of runtime licenses:

Add the Sign Document Code

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

In theSolution Explorer, openProgram.cs. Add the following statements to the using block at the top ofProgram.cs:

C#
usingSystem;usingSystem.IO;usingLeadtools;usingLeadtools.Pdf;

Inside theMain()method, add 3 new string variables namedinputFile,outputFile, andsignatureFile.

C#
staticvoidMain(string[] args){SetLicense();stringinputFile =@"SOURCE PDF FILE PATH";stringoutputFile =@"FILE PATH TO OUTPUT SIGNED PDF TO";stringsignatureFile =@"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"+"//m.ahtuanjie.com/help/sdk/dh/to/lead-compiled-openssl-binaries.html");return;}SignPDFDocument(inputFile, outputFile, signatureFile,"password");巴勒斯坦权力机构rsePDF(outputFile);}

Inside theProgramclass, add a new method namedCheckDigitalSignatureSupportStatus (). Call this method inside theMain()方法在一个条件语句,如上所示。Add the code below to the new method to set the directory to the OpenSSL binaries and check the digital signature support status.

C#
publicstaticboolCheckDigitalSignatureSupportStatus (){RasterDefaults.SetResourceDirectory(LEADResourceDirectory.OpenSSL,@"FILE PATH TO OpenSSL BINARIES DIRECTORY(%LEADTOOLS22_SETUP%\Bin\Dotnet4\x64)");returnPDFDocument.GetDigitalSignatureSupportStatus() == RasterExceptionCode.Success;}

Note

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

Next, add two more methods to theProgramclass namedSignPDFDocument(string inputPdf, string outputPdf, string signatureFile, string password = null)and巴勒斯坦权力机构rsePDF(string filename). Call both of these methods inside theMain()method, as shown above.

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

C#
staticvoidSignPDFDocument(stringinputPdf,stringoutputPdf,stringsignatureFile,stringpassword =null){try{PDFFile inputDoc =newPDFFile(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巴勒斯坦权力机构rsePDF()method to parse the signature inside the PDF, if there is one.

C#
staticvoid巴勒斯坦权力机构rsePDF(stringfilename){PDFDocument doc =newPDFDocument(filename);doc.ParsePages(PDFParsePagesOptions.Signatures, 1, -1);Console.WriteLine();// Act as a spacerforeach(PDFDocumentPage pageindoc.Pages){Console.WriteLine($"Page {page.PageNumber}\n");if(page.Signatures.Count == 0)Console.WriteLine("No signatures on this page\n");foreach(PDFSignature siginpage.Signatures){PrintSignatureValues(sig);}}}

Add a new method to theProgramclass namedPrintSignatureValues(PDFSignature signature). Call this method inside the巴勒斯坦权力机构rsePDF()method, as shown above. Add the code below to thePrintSignatureValues()method to print out the extracted information from thePDFSignatureclass, including the issuer, public key, serial number, subject, valid start and end dates, and the version of the signature.

C#
privatestaticvoidPrintSignatureValues(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 pressingF5, or by selectingDebug -> 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 thePDFFile,PDFDocument,PDFSignature, andPDFDocumentPageclasses.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2023LEAD Technologies, Inc.All Rights Reserved.