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:
- How to: Use a Custom Signer Class to Apply Signatures to the PDF Document
- How to: Use a Custom Timestamp Client to Apply Signatures to the PDF Document
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.