Digital sign a PDF document

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement >

Digital sign a PDF document

In this section we present example code to digitally sign a PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

The example code shows the usage of a PFX file for digital signing.

 

Given below is the sample code.

 

string inputFile = @"unsigned_statement.pdf";
string pfxFile = @"codesigner_whole_chain.pfx";
string pfxPassword = "pfxpassword";
 
// Create a new document object
using (var doc = new PDF())
{
   // Load an existing PDF
   doc.LoadDocument(inputFile);
 
   // Set the signature properties
   var signatureProperties = PDFSignatureProperties.CreateInstance();
   signatureProperties.SetReason(reason: "Approved", labelTextForReason: "Reason");
   signatureProperties.SetLocation(location: "Home", labelTextForLocation: "Location");
   signatureProperties.SetContactInfo(contactInfo: "Bangalore", labelTextForContactInfo: "Contact");
 
   // Create a PDF signature object, signature will be placed on page 1
   var pdfSignature = new PDFSignature(pfxFile, pfxPassword, pageNum: 1, signatureProperties)
   {
      FieldRect = new SimpleRect(4f, 4.8f, 2f, 0.4f),
      IncludeCertOption = System.Security.Cryptography.X509Certificates.X509IncludeOption.EndCertOnly
   };
   // Add the signature to the document
   doc.AddSignature(pdfSignature);
 
   // Save the signed PDF to file
   doc.Save(@"signed_doc.pdf");
}