Digitally sign PDF document with an external signature using C# and VB.NET
The Syncfusion Essential PDF is a feature rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
Using this library, you can sign the PDF document from external digital signature created from various sources such as HSM, USB token, smart card, or other cloud services such as DigiSign.
Digitally sign PDF document with an external signature
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Security.Cryptography; using System.Security.Cryptography.Pkcs; using System.Security.Cryptography.X509Certificates;
VB.NET
Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Security Imports System.Security.Cryptography Imports System.Security.Cryptography.Pkcs Imports System.Security.Cryptography.X509Certificates
4. The following code example shows how to sign the PDF document from external signature.
C#
//Load existing PDF document PdfLoadedDocument document = new PdfLoadedDocument("PDF_Succinctly.pdf"); //Creates a digital signature PdfSignature signature = new PdfSignature(document, document.Pages[0], null, "DigitalSignature"); signature.ComputeHash += Signature_ComputeHash; //Save the PDF document document.Save("ExternalSignature.pdf"); //Close the document. document.Close(true); void Signature_ComputeHash(object sender, PdfSignatureEventArgs arguments) { //Get the document bytes. byte[] documentBytes = arguments.Data; SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true); //Compute the signature using the specified digital ID file and the password. X509Certificate2 certificate = new X509Certificate2("DigitalSignatureTest.pfx", "DigitalPass123"); var cmsSigner = new CmsSigner(certificate); //Set the digest algorithm SHA256 cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1"); signedCms.ComputeSignature(cmsSigner); //Embed the encoded digital signature to the PDF document. arguments.SignedData = signedCms.Encode(); }
VB.NET
'Load existing PDF document Dim document As PdfLoadedDocument = New PdfLoadedDocument("PDF_Succinctly.pdf") 'Creates a digital signature Dim signature As PdfSignature = New PdfSignature(document, document.Pages(0), Nothing, "DigitalSignature") AddHandler signature.ComputeHash, AddressOf Signature_ComputeHash 'Save the PDF document document.Save("ExternalSignature.pdf") 'Close the document document.Close(True) Private Sub Signature_ComputeHash(ByVal sender As Object, ByVal arguments As PdfSignatureEventArgs) 'Get the document bytes Dim documentBytes As Byte() = arguments.Data Dim signedCms As SignedCms = New SignedCms(New ContentInfo(documentBytes), detached:=True) 'Compute the signature using the specified digital ID file and the password Dim certificate As X509Certificate2 = New X509Certificate2("DigitalSignatureTest.pfx", "DigitalPass123") Dim cmsSigner = New CmsSigner(certificate) 'Set the digest algorithm SHA256 cmsSigner.DigestAlgorithm = New Oid("2.16.840.1.101.3.4.2.1") signedCms.ComputeSignature(cmsSigner) 'Embed the encoded digital signature to the PDF document arguments.SignedData = signedCms.Encode() End Sub
A complete work sample for digitally signed PDF document with an external signature can be downloaded from DigitallySignatureSample.ZIP.
By executing the example, you will get the following PDF document.
Take a moment to peruse the documentation, where you will find other options like digitally sign a pdf file, digitally sign an existing pdf document ,remove the digital signature from an existing pdf document and more with code examples.
Click here to explore the rich set of Syncfusion Essential PDF features.
See Also:
How to digitally sign an existing PDF document using C# and VB.NET
How to digitally sign a PDF file in C#, VB.NET
How to apply one or more digital signatures to a PDF using C# and VB.NET
PDF digital signature and timestamp in .NET
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.