Skip to main content Skip to footer

Add Digital and Visual Signatures to PDF Documents in C# .NET

Add Digital and Visual Signatures to PDF Documents in C# .NET

Signatures have always played a significant role in authenticating the validity of documents. In the past, the signature was added using the ink-and-paper based approach. Now, most of the documents are created and shared in a PDF format.

Even the signing process has become digitized, and PDF documents are signed using Digital Signature. The signing of a document using a digital signature does not require the signing authority to be physically present. Instead, the individual/enterprise can create a DigitalID, which acts as their electronic identity and has been verified by the trusted authorities.

This DigitalID is then used to add digital signatures to the document.

Document Solutions for PDF (DsPdf, previously GcPdf) is a cross-platform library used to create, analyze, and modify PDF documents. Document Solutions for PDF supports the digital signature feature and is PAdES b-B and PAdES B-T compliant.. It can be used to sign PDF documents using the relevant API members.

This post shows you how Document Solutions for PDF can generate signed PDF documents by adding digital and visual signatures.

Download a Trial of Document Solutions for PDF Today!

Using Digital Signatures

Digital signatures are commonly used for authenticating PDF documents in almost every enterprise or business, whether it is a government sector, banking, software, tax computation, business contracts, or agreements.

The ability to digitally sign documents offers numerous advantages, such as saving on the paper, efficient utilization of time, providing added security, and remotely signing a document. DsPdf enables you to sign documents digitally -- without Adobe Acrobat dependencies.

In this example, we'll use the signing of a house rent agreement. Both the tenant and landlord are currently residing in two different cities, so the firm involved in setting up this deal has suggested both the parties to sign the rent agreement using digital signatures.

The real estate company involved will share an online utility for signing the document, as there is a high probability of both the ends not having Adobe Acrobat for signing the PDF document.

The image below depicts an unsigned house rent agreement between the tenant and the landlord:

Add Digital and Visual Signatures to PDF Documents in C# .NET

Digital Signature

Digital Signature is a certificate-based electronic Signature, which assures that the signed document has been verified by a trusted authority (and has not been altered).

Adding a digital signature
  1. Create an instance of the GcPdfDocument class to load the PDF document, which is to be signed.
  2. Initialize an instance of X509Certificate2 class to create a certificate that would hold your DigitalID in the form of .pfx file.
  3. Configure the signature properties such as certificate, location, and signer name by initializing an instance of the SignatureProperties class and assigning the valid values to the class properties.
  4. Add a signature field to the PDF document by creating an instance of the SignatureField class. This instance is configured to specify the location, size, and appearance of the signature in the PDF document.
  5. After configuring the SignatureField as per your requirements, assign it to the SignatureField property of the SignatureProperties class instance created above.
  6. Invoke the Sign method of the GcPdfDocument class by passing in the SignatureProperties field and a readable stream as parameters. The Sign method would perform the atomic operation of signing and saving the signed document to the stream.

Here is the code snippet implementing the steps defined above:

public FileStream AddDigitalSignature(Stream stream)
{
   //Load the PDF document to be signed
   GcPdfDocument doc_tenant = new GcPdfDocument();
   doc_tenant.Load(stream);

   //Init a test certificate:
   var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx");
   X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
   X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

   //Add Digital Signature

   //Configure Signature properties
   SignatureProperties spt = new SignatureProperties();
   spt.Certificate = cert;
   spt.Location = "Los Angeles, California";
   spt.SignerName = "Kevin Jade";

   //Init a signature field to hold the signature:           
   SignatureField sf_tenant = new SignatureField();
   sf_tenant.Widget.Rect = new RectangleF(51, 610, 149, 42);
   sf_tenant.Widget.Page = doc_tenant.Pages[1];
   sf_tenant.Widget.BackColor = Color.LightSeaGreen;
   sf_tenant.Widget.DefaultAppearance.Font = StandardFonts.Helvetica;

   //Add the signature field to the document:
   doc_tenant.AcroForm.Fields.Add(sf_tenant);

   //Connect the signature field and signature props:
   spt.SignatureField = sf_tenant;

   // Sign and save the document:
   // NOTES:
   // - Signing and saving is an atomic operation, the two cannot be separated.
   // - The stream passed to the Sign() method must be readable.
   FileStream tenant_signed = new FileStream("TenantSigned.pdf", FileMode.Create);
   doc_tenant.Sign(spt, tenant_signed);

   return tenant_signed;
}

The code snippet above loads the house rent agreement and adds the tenant's signature using a digital signature. After execution, the code would generate the following PDF document signed by the tenant:

Add Digital and Visual Signatures to PDF Documents in C# .NET

Visual Signature

A digital signature embedded with an image or logo is known as a visual signature. So, if the signer has an image of his signature, he can add it to the digital signature and customize its appearance to resemble an ink and paper-based signature.

Therefore, Visual Signature lets you specify additional information with the digital signature.

Adding a Visual Signature

The steps to add Visual Signature to a PDF document using DsPdf API remains the same as described above for the Digital signature; the only change would be assigning an image to the SignatureProperties class instance, which would add an image to the digital signature.

To implement a visual signature, you need to set the Image property of the SignatureAppearance class to an image file that holds the signer's signature.

This can be set by accessing the SignaureAppearance property of the SignatureProperties class.

Here is the code snippet implementing the steps defined above:

public FileStream AddVisualSignature(Stream stream)
{
   //Load the PDF document to be signed
   GcPdfDocument doc_landlord = new GcPdfDocument();
   doc_landlord.Load(stream);

   //Init a test certificate:
   var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx");
   X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq",
   X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

   //Add Visual Signature

   //Configure Signature properties
   SignatureProperties spl = new SignatureProperties();
   spl.Certificate = cert;
   spl.Location = "Los Angeles, California";
   spl.SignerName = "Kevin Jade";
   //Add an image representing the signature:
   spl.SignatureAppearance.Image = Image.FromFile(Path.Combine("Resources", "Misc", "LandlordSign.png"));
   spl.SignatureAppearance.CaptionImageRelation = GrapeCity.Documents.Pdf.Annotations.CaptionImageRelation.ImageOnly;

   //Init a signature field to hold the signature:           
   SignatureField sf_landlord = new SignatureField();
   sf_landlord.Widget.Rect = new RectangleF(348, 610, 150, 42);
   sf_landlord.Widget.Page = doc_landlord.Pages[1];
   sf_landlord.Widget.BackColor = Color.LightSeaGreen;
   sf_landlord.Widget.TextFormat.Font = StandardFonts.Helvetica;

   //Add the signature field to the document:
   doc_landlord.AcroForm.Fields.Add(sf_landlord);

   //Connect the signature field and signature props:
   spl.SignatureField = sf_landlord;

   // Sign and save the document:
   // NOTES:
   // - Signing and saving is an atomic operation, the two cannot be separated.
   // - The stream passed to the Sign() method must be readable.
   FileStream landlord_signed = new FileStream("LandlordSigned.pdf", FileMode.Create);
   doc_landlord.Sign(spl, landlord_signed);

   return landlord_signed;
 }

The code snippet above loads the house rent agreement signed by the tenant and adds the landlord's signature using a visual signature.

After execution, the code would generate the following PDF document now signed by both the tenant and the landlord:

Signature Handling

The DsPdf API supports the following features to handle the signatures added to the Pdf document:

Incremental Update

Many times, a PDF document must be signed by multiple people. In this situation, it is mandatory to make sure that multiple signatures can be added to the document without invalidating any of the other signatures.

The first signature must remain valid when adding the second signature. This scenario is handled by the Incremental Update feature, making sure that multiple signatures can be added to the PDF document.

You can refer to the demo for the implementation details.

Signature Appearance

The appearance of a digital signature or visual signature can be customized using DsPdf API. You can either use the SignatureProperties to set the basic styling properties such as Backcolor, Border, and TextFormat.

For advanced customization, you can use the Widget Annotation to set a FormX object with custom view settings in the SignatureField; it has a custom appearance for the Signature field.

You can have a quick look at the implementation through this demo.

Remove Signatures/SignatureField

The DsPdf API allows you to remove the Signature or SignatureField added to the PDF document. The RemoveAt method can be used to remove the SignatureField while the SignatureField value can be set to null, in case you look forward to removing the signature but keep the field intact.

See this feature in action - visit the removeSignature and removeSignatureFields demo.

Download Sample Application

Download a Trial of Document Solutions for PDF Today!


comments powered by Disqus