Back to Devexpress

TsaClient Class

officefileapi-devexpress-dot-office-dot-tsp.md

latest6.3 KB
Original Source

TsaClient Class

Allows you to generate timestamps.

Namespace : DevExpress.Office.Tsp

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

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public class TsaClient :
    ITsaClient
vb
Public Class TsaClient
    Implements ITsaClient

Remarks

The TsaClient class is the default ITsaClient implementation. Use this class to generate timestamps.

Pass the URI of a timestamp server to the TsaClient constructor. You can specify the username and a password to log into the server. If the server rejects the timestamp request, a TspValidationException is thrown.

Use one of the HashAlgorithmType enumeration values to specify the timestamp’s hashing algorithm. Create the IDigestCalculator implementation and pass the newly created object to the TsaClient constructor to provide the digest value calculator to the timestamp.

Add a Timestamp to the PKCS#7 Signature

The code sample below creates a PKCS#7 signature with a timestamp. The FreeTSA server is used for demonstration purposes as a free trusted TSA client. You may wish to use another client as required.

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

using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    // Create a timestamp:
    ITsaClient tsaClient = new TsaClient(new Uri(@"https://freetsa.org/tsr"), 
        HashAlgorithmType.SHA256);

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

    // Apply the signature to an existing form field:
    var santuzzaSignature = new PdfSignatureBuilder(pkcs7Signature, "Sign");

    // Specify an image and signer information:
    santuzzaSignature.SetImageData(System.IO.File.ReadAllBytes("Signing Documents/SantuzzaValentina.jpg"));
    santuzzaSignature.Location = "Australia";
    santuzzaSignature.Name = "Santuzza Valentina";
    santuzzaSignature.Reason = "I Agree";

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

Using signer = New PdfDocumentSigner("Document.pdf")
  ' Create a timestamp:
  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/certificate.pfx", "123",
                                        HashAlgorithmType.SHA256, tsaClient, Nothing,
                                        Nothing, PdfSignatureProfile.PAdES_BES)

  ' Apply the signature to an existing form field:
  Dim santuzzaSignature = New PdfSignatureBuilder(pkcs7Signature, "Sign")

  ' Specify an image and signer information:
  santuzzaSignature.SetImageData(System.IO.File.ReadAllBytes("Signing Documents/SantuzzaValentina.jpg"))
  santuzzaSignature.Location = "Australia"
  santuzzaSignature.Name = "Santuzza Valentina"
  santuzzaSignature.Reason = "I Agree"

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

Create a Document-Level Timestamp

The code sample below creates a document-level timestamp and applies it to a signature form field. The FreeTSA server is used for demonstration purposes as a free trusted TSA client. You may wish to use another client as required.

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

using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    // Create a timestamp:
    ITsaClient tsaClient = new TsaClient(new Uri(@"https://freetsa.org/tsr"), HashAlgorithmType.SHA256);

    // Create a new signature form field:
    var signatureFieldInfo1 = new PdfSignatureFieldInfo(1);
    signatureFieldInfo1.Name = "SignatureField1";
    signatureFieldInfo1.SignatureBounds = new PdfRectangle(200, 200, 250, 250);

    // Create a document-level timestamp:
    PdfTimeStamp pdfTimeStamp = new PdfTimeStamp(tsaClient);

    // Apply this timestamp to the form field:
    var timeStampSignature = new PdfSignatureBuilder(pdfTimeStamp, signatureFieldInfo1);

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

Using signer = New PdfDocumentSigner("Document.pdf")
  ' Create a timestamp:
  Dim tsaClient As ITsaClient = New TsaClient(New Uri("https://freetsa.org/tsr"), HashAlgorithmType.SHA256)

  ' Create a new signature form field:
  Dim signatureFieldInfo1 = New PdfSignatureFieldInfo(1)
  signatureFieldInfo1.Name = "SignatureField1"
  signatureFieldInfo1.SignatureBounds = New PdfRectangle(200, 200, 250, 250)

  ' Create a document-level timestamp:
  Dim pdfTimeStamp As New PdfTimeStamp(tsaClient)

  ' Apply this timestamp to the form field:
  Dim timeStampSignature = New PdfSignatureBuilder(pdfTimeStamp, signatureFieldInfo1)

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

Implements

ITsaClient

Inheritance

Object TsaClient

See Also

TsaClient Members

DevExpress.Office.Tsp Namespace