How to Add Digital Signature to PDF in C#

This basic guide focuses on how to add digital signature to PDF in C#. The digital signing feature is useful to verify PDF contents as well as to avoid any modifications. These precise steps and the code sample explain how using C# add signature to PDF file.

Steps to Add Digital Signature to PDF in C#

  1. Add the reference to Aspose.PDF for .NET library to digitally sign PDF in your application
  2. Load the input PDF document and the certificate file in PKCS7 class object
  3. Set the rectangular coordinates on the page and the appearance image to place the signature
  4. Certify the PDF file to complete the document signing process
  5. Save the output PDF file having signature at the specified position

These steps clarify how using C# add signature to PDF file. You can customize the appearance image as handwritten signatures while loading the input image. Moreover, the certificate file is also required along with its password to certify the authenticity of the digital signatures.

Code to Add Digital Signature to PDF in C#

using Aspose.Pdf;
namespace AddDigitalSignatureToPDFInCSharp
{
class Program
{
static void Main(string[] args) // Main function to add digital signature to PDF in CSharp
{
// Instantiate the license object to avoid trial version limitations and watermark in output PDF file
Aspose.Pdf.License licDigitalSignature= new Aspose.Pdf.License();
licDigitalSignature.SetLicense("Aspose.Pdf.lic");
// Load the source PDF document for adding the digital signature
Document doc = new Document("input.pdf");
// Instantiate the PdfFileSignature for the loaded PDF document
Aspose.Pdf.Facades.PdfFileSignature signature = new Aspose.Pdf.Facades.PdfFileSignature(doc);
// Load the certificate file along with the password
Aspose.Pdf.Forms.PKCS7 pkcs = new Aspose.Pdf.Forms.PKCS7("certificate1.pfx", "123456789");
// Assign the access permissions
Aspose.Pdf.Forms.DocMDPSignature docMdpSignature = new Aspose.Pdf.Forms.DocMDPSignature(pkcs, Aspose.Pdf.Forms.DocMDPAccessPermissions.FillingInForms);
// Set the rectangle for the signature placement
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(150, 650, 450, 150);
// Set signature appearance
signature.SignatureAppearance = "aspose-logo.png";
// Sign the PDF file with the certify method
signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
// Save digitally signed PDF file
signature.Save("Digitally Signed PDF.pdf");
System.Console.WriteLine("Done");
}
}
}

The code snippet above shows how using C# digitally sign PDF feature can be integrated into your applications. PdfFileSignature class object is used to work with different properties of the signatures. For example, revisions or user rights information, access permissions in order to add or remove the digital signatures from a PDF file, and so on.

In this article, we have learned how to add digital signature to PDF in C#. However, if you want to learn to protect the PDF files, please refer to the article on how to protect PDF with Password in C#.

 English