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 Sign Documents article for more information.
Important
The Pdf
Use the API from the table below to create a visual signature and specify the signer information.
Member | Description |
---|---|
Pdf |
Represents 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 code sample below shows how to create 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 code sample below shows how to use 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", PdfHashAlgorithm.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 });
}