A digital electronic signature is a method of authenticating the authenticity of a document. We will create step-by-step instructions to add digital signature to PDF in C# in this article. We will also create an example for using digital signature in C# by following the comprehensive steps. The whole method for signing PDF documents with digital signatures is outlined here, along with a sample code.
Steps to Add Digital Signature to PDF in C#
- Install GroupDocs.Signature for .NET from NuGet package manager
- Add a reference to the required namespaces for implementation of a digital signature
- Create an object of the Signature class and load the input PDF document
- Create an instance of the DigitalSignOptions class with the required certificate and its password
- Call the Sign method of the Signature class and pass the output PDF file along with the DigitalSignOptions
By following the above points, you can quickly create the C# digital signature feature with a few lines of code. First, set up the required package from the NuGet package manager and include the necessary namespace in the code. In the next step, initialize the Signature class for loading the source PDF, create an instance of the DigitalSignOptions class and pass the certificate file. Finally, add a digital signature to a PDF document and store it on a disc by calling the Sign method.
Code to Add Digital Signature to PDF in C#
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using GroupDocs.Signature; | |
using GroupDocs.Signature.Domain; | |
using GroupDocs.Signature.Options; | |
namespace AddDigitalSignatureToPdfInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to add Digital signature to PDF using C# | |
{ | |
// Remove the watermark in output PDF document by adding license | |
string licensePath = "GroupDocs.Signature.lic"; | |
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License(); | |
lic.SetLicense(licensePath); | |
// load the source PDF for sign with digital signature | |
Signature signature = new Signature("sample.pdf"); | |
// initialize digital option with certificate file path | |
DigitalSignOptions options = new DigitalSignOptions("MrSmithSignature.pfx") | |
{ | |
// set signature position | |
Left = 100, | |
Top = 100, | |
// set | |
Password = "1234567890" | |
}; | |
// sign document to file | |
signature.Sign("signed.pdf", options); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
By following the comprehensive instructions provided in the preceding part, we have demonstrated the digital signature implementation in c#. We have set a few attributes for the signature, including its left and top position. This sample code, on the other hand, can be used as a starting point for adding properties like Contact, Reason, Location, Visible, and many others.
In this topic, we discussed the process to add a digital signature in PDF using C#. Recently, we written an article on How to Sign PDF Document with Barcode signature in C#, have a look on it for more information.