Signing Document
- 4 minutes to read
This topic explains why it is necessary to sign a document and how this can be done using the PDF Document API component.
Overview
The PDF Document API allows you to electronically sign a document. A digital signature is used to help authenticate the identity of a user and document content.
To sign a document, a digital certificate is required. This certificate is issued by a Certification Authority (CA). The role of the CA is to validate the holder’s identity and provide access to a certificate.
To learn how to apply a signature to a document, refer to the How to Sign document section.
Prerequisite
Before signing, the PDF Document API component must have a document (e.g., you can load a document using the PdfDocumentProcessor.LoadDocument method).
How to Sign
Access a signing certificate.
You can either get it from a third-party certificate authority (CA), or generate the X.509 certificate with a private key and public key pair using, e.g, the Makecert.exe (Certificate Creation Tool) for testing purposes only.
Create a visual signature and specify the signing information.
Visual signatures are document widgets that link digital signatures with logos, photos and text. Users can embed these visuals into documents to help readers see who signed the PDF and to obtain additional non-visual signature data.
A signature is represented by an instance of the PdfSignature object with the specified certificate and other settings. The signature can be accessed using the PdfSaveOptions.Signature property.
You can create a visual signature using one of the PdfSignature constructor overloads that takes 4 arguments. For example, using a certificate, image data represented by a byte array, and specifying the page number and signature bounds. The signature bounds are represented by a PdfOrientedRectangle object. You can specify the rotation angle for the signature (in radians) when creating a PdfOrientedRectangle object. A positive angle means counterclockwise rotation; a negative angle means clockwise rotation.
X509Certificate2 certificate = new X509Certificate2(@"..\..\SignDemo.pfx", "dxdemo");
byte[] imageData = File.ReadAllBytes("..\\..\\Signature.png");
int pageNumber = 1;
int angleInDegrees = 45;
double angleInRadians = angleInDegrees * (Math.PI / 180);
PdfOrientedRectangle signatureBounds = new PdfOrientedRectangle(new PdfPoint(0, 460), 250, 90, angleInRadians);
PdfSignature signature = new PdfSignature(certificate, imageData, pageNumber, signatureBounds);
Specify the signing information using the following properties.
Member | Description |
---|---|
PdfSignature.Name | Gets or sets the name of the person or authority signing the document. |
PdfSignature.Location | Gets or sets the CPU host name or physical location of the signature. |
PdfSignature.Reason | Gets or sets the reason for a document signature. |
PdfSignature.ContactInfo | Specifies the contact information which helps a recipient to verify the signature provided by the signer. |
In addition, you can use the PdfSignature.SigningTime property to get a signing time. This property is used to validate the signature.
Save a signed document.
To accomplish this task, call the PdfDocumentProcessor.SaveDocument method and pass the PdfSaveOptions object containing a signature as a parameter.
Example
The following example shows how to sign a document with a visual signature.
Note
Show Me The complete sample project is available at https://github.com/DevExpress-Examples/how-to-add-a-digital-signature-into-a-pdf-document-t243905
using System.IO;
using System.Security.Cryptography.X509Certificates;
using DevExpress.Pdf;
namespace PDFSignature {
class Program {
static void Main(string[] args) {
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
documentProcessor.LoadDocument(@"..\..\Document.pdf");
X509Certificate2 certificate = new X509Certificate2(@"..\..\SignDemo.pfx", "dxdemo");
byte[] imageData = File.ReadAllBytes("..\\..\\image.emf");
int pageNumber = 1;
PdfRectangle signatureBounds = new PdfRectangle(460, 400, 560, 580);
PdfSignature signature = new PdfSignature(certificate, imageData, pageNumber, signatureBounds);
signature.Location = "USA";
signature.ContactInfo = "john.smith@example.com";
signature.Reason = "Approved";
documentProcessor.SaveDocument(@"..\..\SignedDocument.pdf", new PdfSaveOptions() { Signature = signature });
}
}
}
}