Back to Devexpress

Pkcs7Signer Class

officefileapi-devexpress-dot-pdf-dot-pkcs7signer.md

latest5.9 KB
Original Source

Pkcs7Signer Class

Allows you to create PKCS#7 signatures.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class Pkcs7Signer :
    Pkcs7SignerBase
vb
Public Class Pkcs7Signer
    Inherits Pkcs7SignerBase

Remarks

Use a Pkcs7Signer object to create a PKCS#7 signature.

Pass the Pkcs7Signer object to the PdfSignatureBuilder object constructor to apply a PKCS#7 signature to the form field.

Tip

You can use the Pkcs7Signer object when you use PdfDocumentProcessor to sign a document. Refer to the following article for a code sample. Note that this approach has limitations: How to: Use PdfDocumentProcessor to Add a Visual Signature to a Document

Create a Signature

Specify a certificate and a password to create a signature. The PDF Document API supports .pfx files with an X.509 certificate.

You can also specify the signature’s hash algorithm. PDF Document API supports SHA1, SHA256, SHA384 and SHA512 algorithm types.

Note

The SHA1 hash type may affect the signature’s integrity, authenticity, and legal validity.

The code sample below shows how to create a PKCS#7 signature:

csharp
Pkcs7Signer pkcs7Signature =
    new Pkcs7Signer("Signing Documents//testcert.pfx", "123", HashAlgorithmType.SHA256);
vb
Dim pkcs7Signature As New Pkcs7Signer("Signing Documents//testcert.pfx", "123", HashAlgorithmType.SHA256)

Add a Timestamp to a Signature

Use the TsaClient object to create a timestamp for a signature. You can create a ITsaClient implementation to use a custom timestamp client.

The code sample below shows how to create a signature with a timestamp:

csharp
ITsaClient tsaClient = new TsaClient(new Uri(@"https://freetsa.org/tsr"), HashAlgorithmType.SHA256);
Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Signing Documents//testcert.pfx", "123", HashAlgorithmType.SHA256, tsaClient);
vb
Dim tsaClient As ITsaClient = New TsaClient(New Uri("https://freetsa.org/tsr"), HashAlgorithmType.SHA256)
Dim pkcs7Signature As New Pkcs7Signer("Signing Documents//testcert.pfx", "123", HashAlgorithmType.SHA256, tsaClient)

Create a PAdES Signature

Use the PdfSignatureProfile enumeration to create a signature with a PAdEs baseline profile. Pass the PdfSignatureProfile.PAdES_BES field to the Pkcs7Signer object, as shown below:

csharp
using DevExpress.Pdf;
using DevExpress.Office.DigitalSignatures;
using DevExpress.Office.Tsp;

using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    IOcspClient ocspClient = new OcspClient();
    ICrlClient crlClient = new CrlClient();

    // the freetsa client is used for demonstration purposes
    ITsaClient tsaClient = new TsaClient(new Uri(@"https://freetsa.org/tsr"), HashAlgorithmType.SHA256);

    // Create a PKCS#7 signature:
    Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Signing Documents//testcert.pfx", "123",
     HashAlgorithmType.SHA256, tsaClient, ocspClient, crlClient, PdfSignatureProfile.PAdES_BES);

    // Create a new signature field:
    var signatureFieldInfo = new PdfSignatureFieldInfo(1);
    signatureFieldInfo.Name = "SignatureField";
    signatureFieldInfo.SignatureBounds = new PdfRectangle(10, 10, 150, 150);

    // Apply a signature with a new signature:
    var signature =
    new PdfSignatureBuilder(pkcs7Signature, signatureFieldInfo);

    // Sign and save the document:
    signer.SaveDocument("SignedDocument.pdf", signature);
}
vb
Imports DevExpress.Pdf
Imports DevExpress.Office.DigitalSignatures
Imports DevExpress.Office.Tsp

Using signer = New PdfDocumentSigner("Document.pdf")
  Dim ocspClient As IOcspClient = New OcspClient()
  Dim crlClient As ICrlClient = New CrlClient()

  ' the freetsa client is used for demonstration purposes
  Dim tsaClient As ITsaClient = New TsaClient(New Uri("https://freetsa.org/tsr"), HashAlgorithmType.SHA256)

  ' Create a PKCS#7 signature:
  Dim pkcs7Signature As New Pkcs7Signer("Signing Documents//testcert.pfx", "123", HashAlgorithmType.SHA256, tsaClient, ocspClient, crlClient, PdfSignatureProfile.PAdES_BES)

  ' Create a new signature field:
  Dim signatureFieldInfo = New PdfSignatureFieldInfo(1)
  signatureFieldInfo.Name = "SignatureField"
  signatureFieldInfo.SignatureBounds = New PdfRectangle(10, 10, 150, 150)

  ' Apply a signature with a new signature field:
  Dim signature = New PdfSignatureBuilder(pkcs7Signature, signatureFieldInfo)

  ' Sign and save the document:
  signer.SaveDocument("SignedDocument.pdf", signature)
End Using

Implements

IExternalSigner

Inheritance

Object Pkcs7SignerBase Pkcs7Signer

See Also

Pkcs7Signer Members

Sign PDF Documents with PDF Document API

DevExpress.Pdf Namespace