Articles in this section
Category / Section

How to Sign PDF Document Using C# and VB.NET in .NET PDF Library?

6 mins read

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, create forms, compress, and secure PDF files.

Using this library, you can sign PDF documents with an external digital signature created from various sources such as HSM, USB tokens, smart cards, 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 a 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 a PDF document with an 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 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 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 working sample for a 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 the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.



Conclusion

I hope you enjoyed learning about how to sign a PDF document using C# and VB.NET in the .NET PDF Library.

You can refer to our .NET PDF Library feature tour page to learn about its other groundbreaking feature representations. You can also explore our documentation to understand how to present and manipulate data.

For current customers, you can check out our Flutter components on the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our .NET PDF Library.

If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied