Add and Verify Digital Signatures in PDF Documents using C#

Digital Signatures are used to secure PDF documents before they are shared with third parties. Digitally signing a PDF document makes it possible to detect the tampering by verifying the document using the digital signature. In order to secure the PDF documents programmatically using digital signatures, this article will show you how to add and verify digital signatures in a PDF document using C#. Ultimately, you will learn how to:

  • Add digital signatures in PDF documents using C#.
  • Digitally sign PDF documents with a Timestamp server in C#.
  • Verify digital signature in PDF using C#.

C# Library for Digital Signatures in PDF

In order to add and verify digital signatures in PDF documents, we’ll use Aspose.PDF for .NET API which is a powerful PDF manipulation API for creating, editing, converting and digitally signing PDF documents. You can download Aspose.PDF for .NET or install it using one of the following ways in Visual Studio:

NuGet Package Manager

Digitally Sign PDF in C#

Package Manager Console

PM> Install-Package Aspose.PDF

Add Digital Signature to a PDF Document in C#

The following are the steps to sign a PDF document using Aspose.PDF for .NET.

  • Create an object of the Document class and initialize it with the PDF document’s path.
  • Create an object of PdfFileSignature class and initialize it with the Document class’s object.
  • Create an object of PKCS7 class and initialize it with a certificate path and password.
  • Create and initialize the object of DocMDPSignature class for the MDP signature type.
  • Create a Rectangle for signature placement.
  • Digitally sign PDF document using PdfFileSignature.Certify() method.
  • Save the document using PdfFileSignature.Save() method.

The following code sample shows how to add a digital signature to a PDF document in C#.

Add Digital Signature to PDF using Timestamp Server in C#

You can also add a digital signature to a PDF document with the TimeStamp server by providing its details using the TimestampSettings class. The following code sample shows how to digitally sign a PDF document with a TimeStamp server in C#.

Verify Digital Signatures in PDF using C#

The following are the steps to verify the digital signature in a PDF document:

  • Create an object of the Document class and initialize it with the PDF document’s path.
  • Create an object of PdfFileSignature class and initialize it with the Document class’s object.
  • Access all the signatures of the PDF document.
  • Verify the validity of the signature using PdfFileSignature.VerifySigned() method.

The following code sample shows how to verify the digital signature in PDF using C#.

Try Aspose.PDF for .NET for Free

You can get a free temporary license to try and use Aspose.PDF for .NET.

Related Articles