Digital Signatures | |
Digital signatures can be added to a PDF document using PdfDigitalSignatureElement objects. Adding a digital signature requires a PKCS#12 certificate (provided as a .pfx or a .p12 file).
ValidateSignatures checks every signature of a loaded document and returns a PdfSignatureValidationResult for each: whether it is intact (IsValid), whether its certificate chains to a trusted root (IsTrusted), whether content was added to the document after it (CoversWholeDocument), the signing and time-stamp times, the signer and the certificate, and a Status that sums them up. Pass your own certificate authority to trust it as a root, and set RevocationCheckEnabled to ask the certificate's issuer whether it has been revoked.
Each signature is checked against the bytes it was made over, so a signature followed by a later one, or by a form fill, is reported intact.
PdfSecurityManager manager = new PdfSecurityManager(); manager.Load("contract-signed.pdf"); manager.RevocationCheckEnabled = true; // trust the organisation's own certificate authority as well as the system's PdfDigitalCertificatesCollection roots = PdfDigitalCertificatesStore.GetCertificates("our-ca.pfx", "password"); foreach (PdfSignatureValidationResult result in manager.ValidateSignatures(roots)) { Console.WriteLine("{0}: {1}, signed by {2} on {3}", result.SignatureName, result.Status, result.SignerName, result.SigningTime); foreach (string message in result.Messages) Console.WriteLine(" " + message); } manager.Close();
The following sample code shows how to add a digital signature to a pdf document using Select.Pdf Library for .NET.
// create a new pdf document PdfDocument doc = new PdfDocument(); // add a new page to the document PdfPage page = doc.AddPage(); // get image path // the image will be used to display the digital signature over it string imgFile = @"C:\Path\to\logo.png"; // get certificate path string certFile = @"C:\Path\to\certificate.pfx"; // define a rendering result object PdfRenderingResult result; // create image element from file path PdfImageElement img = new PdfImageElement(0, 0, imgFile); result = page.Add(img); // get the #PKCS12 certificate from file PdfDigitalCertificatesCollection certificates = PdfDigitalCertificatesStore.GetCertificates(certFile, "selectpdf"); PdfDigitalCertificate certificate = certificates[0]; // create the digital signature object PdfDigitalSignatureElement signature = new PdfDigitalSignatureElement(result.PdfPageLastRectangle, certificate); signature.Reason = "SelectPdf"; signature.ContactInfo = "SelectPdf"; signature.Location = "SelectPdf"; page.Add(signature); // save pdf document doc.Save("Sample.pdf"); // close pdf document doc.Close();