officefileapi-114053-pdf-document-api-examples-document-protection-how-to-add-a-visual-signature-into-a-pdf-document.md
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 PdfDocumentProcessor removes existing signatures from a document when it is saved. However, if you use PdfDocumentProcessor to apply a signature, it is retained. Use the PdfSignatureBuilder to apply multiple signatures to the document.
Use the API from the table below to create a visual signature and specify the signer information.
| Member | Description |
|---|---|
| PdfSignature | A signature. You can use the X509Certificate2 certificate. Pass the Pkcs7Signer object to the constructor to use the PKCS#7 signature. |
| PdfSignature.Name | Gets or sets the name of the person or authority signing the document. |
| PdfSignature.Location | Gets or sets the signing location. |
| 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. |
| PdfSignature.SigningTime | 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 = "[email protected]";
signature.Reason = "Approved";
// Save a signed document:
documentProcessor.SaveDocument(@"..\..\SignedDocument.pdf", new PdfSaveOptions()
{ Signature = signature });
Imports System.IO
Imports System.Security.Cryptography.X509Certificates
Imports DevExpress.Pdf
' Provide a signature certificate:
Dim certificate As X509Certificate2 = New X509Certificate2("..\..\SignDemo.pfx", "dxdemo")
' Specify the signature's image data and location parameters:
Dim imageData As Byte() = File.ReadAllBytes("..\..\Signature.png")
Dim pageNumber As Integer = 1
Dim angleInDegrees As Integer = 45
Dim angleInRadians As Double = angleInDegrees * (Math.PI / 180)
Dim signatureBounds As PdfOrientedRectangle = New PdfOrientedRectangle(New PdfPoint(0, 460), 250, 90, angleInRadians)
' Pass all instances created above to the PdfSignature constructor:
Dim signature As PdfSignature = New PdfSignature(certificate, imageData, pageNumber, signatureBounds)
' Specify the signing information:
signature.Location = "USA"
signature.ContactInfo = "[email protected]"
signature.Reason = "Approved"
' Save a signed document:
documentProcessor.SaveDocument("..\..\SignedDocument.pdf", New PdfSaveOptions() With
{ 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 = "[email protected]";
signature.Reason = "Approved";
// Save a signed document:
processor.SaveDocument("SignedDocument.pdf", new PdfSaveOptions()
{ Signature = signature });
}
Imports System
Imports DevExpress.Pdf
Imports System.IO
Using processor As New PdfDocumentProcessor()
processor.LoadDocument("Document.pdf")
' Create a PKCS#7 signature
Dim pkcs7Signature As New Pkcs7Signer("Signing Documents//certificate.pfx", "123", HashAlgorithmType.SHA256)
' Specify the signature's image data and location parameters:
Dim imageData() As Byte = File.ReadAllBytes("Signing Documents//JohnSmith.jpg")
Dim pageNumber As Integer = 1
Dim angleInDegrees As Integer = 45
Dim angleInRadians As Double = angleInDegrees * (Math.PI / 180)
Dim signatureBounds As New PdfOrientedRectangle(New PdfPoint(0, 460), 250, 90, angleInRadians)
' Pass all instances created above to the PdfSignature constructor:
Dim signature As New PdfSignature(pkcs7Signature, imageData, pageNumber, signatureBounds)
' Specify the signing information:
signature.Location = "USA"
signature.ContactInfo = "[email protected]"
signature.Reason = "Approved"
Save a signed document:
processor.SaveDocument("SignedDocument.pdf", New PdfSaveOptions() With {.Signature = signature})
End Using
See Also