Back to Devexpress

PdfSignature Class

officefileapi-devexpress-dot-pdf-ae5b8ef6.md

latest7.9 KB
Original Source

PdfSignature Class

An electronic signature used to sign a PDF file.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Core.dll

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class PdfSignature :
    PdfObject
vb
Public Class PdfSignature
    Inherits PdfObject

The following members return PdfSignature objects:

Remarks

The PdfSignature class represents a signature with the CMS/PKCS #7 message and the specified certificate. The PdfDocumentProcessor uses the adbe.pkcs7.detached signature value encoding.

Pass PdfOrientedRectangle object as the constructor’s signatureBounds parameter to specify the signature’s bounds. You can also specify the rotation angle for the signature (in radians) when you create a PdfOrientedRectangle object. A positive angle rotates it counter-clockwise and a negative angle rotates it clockwise.

csharp
//...
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);
vb
'...
Dim certificate As X509Certificate2 = New X509Certificate2("..\..\SignDemo.pfx", "dxdemo")

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)
Dim signature As PdfSignature = New PdfSignature(certificate, imageData, pageNumber, signatureBounds)

The PdfSignature class properties allow you to specify the signature’s name, reason, location, contact information, and signing time.

Pass a PdfSaveOptions object as a parameter with a signature to the PdfDocumentProcessor.SaveDocument method to save a document with signing information.

Use the PdfSaveOptions.Signature property to access a PdfSignature object.

Example

This example illustrates how to add a visual signature to a PDF document.

To accomplish this task, do the following:

csharp
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 = "[email protected]";
                signature.Reason = "Approved";

                // Save the signed document.
                documentProcessor.SaveDocument(@"..\..\SignedDocument.pdf", new PdfSaveOptions() { Signature = signature });
            }
        }
    }
}
vb
Imports DevExpress.Pdf
Imports System
Imports System.IO
Imports System.Security.Cryptography.X509Certificates

Namespace PDFSignature
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Using documentProcessor As PdfDocumentProcessor = New PdfDocumentProcessor()
                documentProcessor.LoadDocument("..\..\Demo.pdf")
                Dim certificate As X509Certificate2 = New X509Certificate2("..\..\SignDemo.pfx", "dxdemo")
                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(620, 210), 250, 90, angleInRadians)
                Dim signature As PdfSignature = New PdfSignature(certificate, imageData, pageNumber, signatureBounds)
                signature.Location = "USA"
                signature.ContactInfo = "[email protected]"
                signature.Reason = "Approved"
                documentProcessor.SaveDocument("..\..\SignedDocument.pdf", New PdfSaveOptions() With {
                    .Signature = signature
                })
            End Using
        End Sub
    End Class
End Namespace

Inheritance

Object DevExpress.Pdf.Native.PdfDocumentItem DevExpress.Pdf.Native.PdfObject PdfSignature

See Also

PdfSignature Members

DevExpress.Pdf Namespace