We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

  1. Create a new C# console application project.

Create a console application in Visual Studio

  1. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET framework application from NuGet.org. Graphical user interface, application

Description automatically generated
  2. 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.

 

Externally sign 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

Note:

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile