How to: Add a Visual Signature into a PDF Document
- 3 minutes to read
Important
You require a license to the DevExpress Office File API or DevExpress Universal Subscription to use these examples in production code. Refer to the DevExpress Subscription page for pricing information.
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
This example illustrates how to add a visual signature to a PDF document.
To accomplish this task, do the following:
- Create a PDF Document API component represented by an instance of the PdfDocumentProcessor class.
- Load a document from a file using the PdfDocumentProcessor.LoadDocument method.
- Create a certificate using a certificate file name and a password to access the certificate.
- Create a document 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.
- Specify signing location, contact info and reason using the PdfSignature.Location, PdfSignature.ContactInfo and PdfSignature.Reason properties, respectively.
- Save the signed document with signing information by calling the PdfDocumentProcessor.SaveDocument method.
using DevExpress.Pdf;
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace PDFSignature {
class Program {
static void Main(string[] args) {
// Create a PDF document processor.
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
// Load a PDF document.
documentProcessor.LoadDocument(@"..\..\Demo.pdf");
// Create a visual signature using certificate, image data, and specifying page number, signature bounds and rotation angle.
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(620, 210), 250, 90, angleInRadians);
PdfSignature signature = new PdfSignature(certificate, imageData, pageNumber, signatureBounds);
// Specify signing location, contact info and reason.
signature.Location = "USA";
signature.ContactInfo = "john.smith@example.com";
signature.Reason = "Approved";
// Save the signed document.
documentProcessor.SaveDocument(@"..\..\SignedDocument.pdf", new PdfSaveOptions() { Signature = signature });
}
}
}
}
See Also