How To: Use PdfDocumentProcessor to Add a Visual Signature to a Document
- 4 minutes to read
Important
You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.
The PdfDocumentProcessor allows you to apply a signature with the CMS/PKCS #7 message and adbe.pkcs7.detached signature value encoding.
Note that you can sign a document only once. Use the PdfDocumentSigner and PdfSignatureBuilder classes to apply multiple signatures. Refer to the following article for more information: Sign Documents
Important
The PdfPdf
to apply a signature, it is retained. Use the Pdf
Use the API from the table below to create a visual signature and specify the signer information.
Member | Description |
---|---|
Pdf |
A signature. You can use the X509Certificate2 certificate. Pass the Pkcs7Signer object to the constructor to use the PKCS#7 signature. |
Pdf |
Gets or sets the name of the person or authority signing the document. |
Pdf |
Gets or sets the signing location. |
Pdf |
Gets or sets the reason for a document signature. |
Pdf |
Specifies the contact information which helps a recipient to verify the signature provided by the signer. |
Pdf |
Gets the time the document was signed. |
The following code snippet creates a signature with the X509 certificate:
using System.IO;
using System.Security.Cryptography.X509Certificates;
using DevExpress.Pdf;
// Provide a signature certificate:
X509Certificate2 certificate = new X509Certificate2(@"..\..\SignDemo.pfx", "dxdemo");
// Specify the signature's image data and location parameters:
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);
// Pass all instances created above to the PdfSignature constructor:
PdfSignature signature = new PdfSignature(certificate, imageData, pageNumber, signatureBounds);
// Specify the signing information:
signature.Location = "USA";
signature.ContactInfo = "john.smith@example.com";
signature.Reason = "Approved";
// Save a signed document:
documentProcessor.SaveDocument(@"..\..\SignedDocument.pdf", new PdfSaveOptions()
{ Signature = signature });
The following code snippet uses the Pkcs7Signer object to create a signature:
using System;
using DevExpress.Pdf;
using System.IO;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
processor.LoadDocument("Document.pdf");
// Create a PKCS#7 signature
Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Signing Documents//certificate.pfx", "123", HashAlgorithmType.SHA256);
// Specify the signature's image data and location parameters:
byte[] imageData = File.ReadAllBytes("Signing Documents//JohnSmith.jpg");
int pageNumber = 1;
int angleInDegrees = 45;
double angleInRadians = angleInDegrees * (Math.PI / 180);
PdfOrientedRectangle signatureBounds = new PdfOrientedRectangle(new PdfPoint(0, 460), 250, 90, angleInRadians);
// Pass all instances created above to the PdfSignature constructor:
PdfSignature signature = new PdfSignature(pkcs7Signature, imageData, pageNumber, signatureBounds);
// Specify the signing information:
signature.Location = "USA";
signature.ContactInfo = "john.smith@example.com";
signature.Reason = "Approved";
// Save a signed document:
processor.SaveDocument("SignedDocument.pdf", new PdfSaveOptions()
{ Signature = signature });
}