PDF Document API v20.1 - Multiple Signatures and Timestamps

Our PDF Document API (v20.1) allows you to apply multiple signatures to a document without existing signatures and to apply new signatures to a document that has yet to be signed.

This new PDF Signature API allows you to execute the following actions:

  • Sign an existing signature field
  • Sign a new field
  • Apply a signature with a timestamp

The PdfDocumentSigner class is an entry point to the Signature API. Use it to save a document with applied signatures. You can pass a single signature or an array of signatures to the PdfDocumentSigner.SaveDocument method.

For a complete sample project, please refer to the following in our GitHub repository: How to Apply Multiple Signatures

Sign an Existing Signature Field

Use the PdfSignatureBuilder class to apply a signature to an existing form field. The Pkcs7Signer class allows you to specify signature validation data (certificate, hash algorithm, etc.). The PDF Document API supports PKCS#7 signatures with X.509 certificates.

//Load a document to sign:
using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    //Create a PKCS#7 signature:
    Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Documents/testcert.pfx", "123", 
    PdfHashAlgorithm.SHA256);

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

    //Sign and save the document:
    signer.SaveDocument("SignedDocument.pdf", signature);
}

Sign a New Field

Use the PdfSignatureFieldInfo class to specify information about a new form field (name, location on the page, etc.) and pass it to the PdfSignatureBuilder, as shown below:

//Load a document to sign
using (var signer = new PdfDocumentSigner("Document.pdf"))
{
    //Create a PKCS#7 signature:
    Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Documents/testcert.pfx", "123", 
    PdfHashAlgorithm.SHA256);

    //Specify the name and location of a new signature field
    var signatureFieldInfo = new PdfSignatureFieldInfo(1);
    signatureFieldInfo.Name = "SignatureField";
    signatureFieldInfo.SignatureBounds = new PdfRectangle(10, 10, 150, 150);
    signatureFieldInfo.RotationAngle = PdfAcroFormFieldRotation.Rotate90;

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

    //Sign and save the document
    signer.SaveDocument("SignedDocument.pdf", signature);
}

Apply a Timestamp to a Signature

The TsaClient class allows you to generate a timestamp. Pass its instance to the Pkcs7Signer constructor to apply the timestamp to a signature.

//Create a timestamp
ITsaClient tsaClient = new PdfTsaClient(new Uri(@"https://freetsa.org/tsr"), PdfHashAlgorithm.SHA256);

//Create a PKCS#7 signature with a timestamp:
Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Documents/testcert.pfx", "123", PdfHashAlgorithm.SHA256, tsaClient);

Create a Custom Signer and a Timestamp Client

If you want to implement a custom signer, you can declare a descendant of the Pkcs7SignerBase class.

Implement the ITsaClient interface to use a custom timestamp client. Sample projects with complete code are available on GitHub:

Use PdfDocumentProcessor to Sign Documents

We enhanced our old approach, so you can use PdfDocumentProcessor to sign a document with a PKCS#7 signature.

    
  using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
  {
      processor.LoadDocument("Document.pdf");
            
      //Create a PKCS#7 signature
      Pkcs7Signer pkcs7Signature = new Pkcs7Signer("Documents/certificate.pfx", "123", PdfHashAlgorithm.SHA256);

      //Specify the signature's image data and location parameters:
      byte[] imageData = File.ReadAllBytes("Documents/JohnSmith.jpg");
      int pageNumber = 1;
      PdfOrientedRectangle signatureBounds = new PdfOrientedRectangle(new PdfPoint(0, 460), 250, 90);

      //Pass all instances created above to the PdfSignature constructor:
      PdfSignature signature = new PdfSignature(pkcs7Signature, imageData, pageNumber, signatureBounds);

      //Save the signed document:
      processor.SaveDocument("SignedDocument.pdf", new PdfSaveOptions()
      { Signature = signature });
  }
    

Please note that the PdfDocumentProcessor class has the following limitations:

  • You can sign a document only once.
  • Existing signatures are removed from a document when saved. However, if you use PdfDocumentProcessor to apply a signature, it is retained.

Feedback

As always, we welcome your thoughts. Please comment below and let us know what you think about this new feature.

What’s New in v20.1

To explore the scope, breadth and capabilities of our new products/features, please visit: https://www.devexpress.com/Subscriptions/New-2020-1.xml.
7 comment(s)
Tobias Johansson
Tobias Johansson
And certificate signature placeholder?
8 June 2020
Alex B.G. (DevExpress)
Alex B.G. (DevExpress)

@Tobias Johansson
I hope I got understood your question. I assume you are referring to Reporting:
T828850 - PDF export with place holder for signing by clicking

Our Office File API has the required functionality needed to complete this feature. Our Reporting Team has published their Road Map and this feature is listed (see Visual Signatures). Please let me know if I misunderstood your question.

10 June 2020
Tobias Johansson
Tobias Johansson
Yes you are right, I was thinking about reporting. thanks
10 June 2020
Juan José López
Juan José López
how can I show a visual timestamp?
12 June 2020
Alex B.G. (DevExpress)
Alex B.G. (DevExpress)
@Juan José López
If you want to visualize a time stamp near a signature on a document, the best way would be to render text into a signature bitmap using System.Drawing.Graphics. Then use the generated image during the signing process.
You can obtain date\time for visualization via DateTime.Now() or NTP protocol.
This way will allow you to retain the information on the page and keep it consistent just in case you delete the signature. The time stamp information will also be deleted from the page.
If you have any further questions on this topic please contact us via the DevExpress Support Center so that we can help you efficiently.

16 June 2020
SharePoint Solutions s.r.o.
SharePoint Solutions s.r.o.

Hi, this is fantastic job!

Is there a chance to add just the timestamp to a LTV enabled document which is already signed?

17 June 2020
Alex B.G. (DevExpress)
Alex B.G. (DevExpress)

@SharePoint Solutions s.r.o.
Our API does not allow you to add a timestamp to an existing signature. If you try to do so, it is very likely that the signature will be invalidated. The most appropriate way would be to re-sign the document or append it with a new signature and a new timestamp.

The PDF-2.0 specification allows you to add a document level timestamp. This function is not currently supported by our API. If that’s what you require, please contact us via the DevExpress Support Center, so we can better understand your business requirements.

17 June 2020

Please login or register to post comments.