Click or drag to resize

Digital Signature Element

Digital signatures are added to a PDF document with the DigitalSignatureElement, placed on a page of a new document or of an existing one. The element is drawn in its rectangle with the name of the signer, the reason, the location and the date, and the document is signed when it is saved. The signature is computed with SHA-256 by default (DigestAlgorithm).

The signing certificate, with its private key, is a DigitalCertificate obtained from DigitalCertificatesStore: GetCertificates(String, String) reads a password protected PKCS#12 file (.pfx or .p12), on every platform, and GetCertificates(CertSystemStore) reads a certificate store of the system. The returned DigitalCertificatesCollection lists the certificates that hold a private key first, and finds a certificate with GetCertBySubject(String), GetCertByIssuer(String) or GetCertBySerialNumber(Byte).

A key held outside the application - in a hardware security module, a smart card, a USB token or a key service - signs through an IExternalSigner passed to the other constructor of the element: CertificateStoreSigner for a certificate of the system store, Pkcs11Signer for a PKCS#11 device, or an implementation of your own. With EnableLtv set, the revocation data of the certificates is embedded when the document is saved, so the signature can be validated later without going online. SignatureBackgroundImage draws a logo or a seal behind the text of the signature.

A document gets one new signature per save. To sign a document a second time, save it, load the saved file and add the next signature: each signature then covers its own revision of the document and all of them stay valid. Signing an existing PDF file with a timestamp and validating the signatures of a document are done with the PDF security manager.

Sample Code

Below there is a sample showing how to add a digital signature to a PDF document:

// add a page to the PDF document
PdfPage firstPage = document.AddPage();

string logoImagePath = Path.Combine("img", "logo.jpg");
string certificateFilePath = "certificate.pfx";

// create the area where the digital signature is displayed in the PDF document;
// in this sample the area is a logo image but it could be anything else
ImageElement logoElement = new ImageElement(0, 0, logoImagePath);
AddElementResult addResult = firstPage.AddElement(logoElement);

// get the PKCS#12 certificate from file
DigitalCertificatesCollection certificates =
    DigitalCertificatesStore.GetCertificates(certificateFilePath, "password");
DigitalCertificate certificate = certificates[0];

// create the digital signature over the logo image element
DigitalSignatureElement signature = new DigitalSignatureElement(
    addResult.EndPageBounds, certificate);
signature.Reason = "Protect the document from unwanted changes";
signature.ContactInfo = "The contact email is office@example.com";
signature.Location = "Development server";
firstPage.AddElement(signature);

// the document is signed when it is saved
document.Save("signed.pdf");
See Also