- Create and Verify Digital Signatures using .NET Digital Signature Library
- .NET Digital Signature Library Code Samples
- Download .NET Digital Signature Library with all samples
void AdvancedPdfSignature(string unsignedDocument, string signedDocument)
{
PdfSignature ps = new PdfSignature("");
//load the PDF document
ps.LoadPdfDocument(File.ReadAllBytes(unsignedDocument));
//Load the signature certificate from a PFX or P12 file
//ps.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate("cert.pfx", "123456");
//Load the certificate from Microsoft Store.
//The smart card or USB token certificates are usually available on Microsoft Certificate Store (start - run - certmgr.msc).
//If the smart card certificate not appears on Microsoft Certificate Store it cannot be used by the library
ps.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.Thumbprint, "thumbprint", true);
//The smart card PIN dialog can be bypassed for some smart cards/USB Tokens.
DigitalCertificate.SmartCardPin = "123456";
ps.SigningReason = "I approve this document";
ps.SigningLocation = "Accounting department";
//custom digital signature rectangle position
ps.SignatureAdvancedPosition = new System.Drawing.Rectangle(10, 10, 400, 150);
//add an image to the digital signature rectangle
ps.SignatureImage = File.ReadAllBytes("signature_image.jpg");
ps.SignatureImageType = SignatureImageType.ImageAndText;
//customize the digital signature text
ps.SignatureText = "Siganture created by: " + ps.DigitalSignatureCertificate.Subject
+ Environment.NewLine + "Date: " + DateTime.Now.ToString("dd.MM.yyyy HH:mm");
//add green tick
ps.OldStyleAdobeSignature = true;
//digital signature hash algorithm
ps.HashAlgorithm = SignLib.HashAlgorithm.SHA256;
//PAdES signature. Requires Adobe X
ps.SignatureStandard = SignLib.Pdf.PdfSignatureStandard.Cades;
//add document certification
ps.CertifySignature = CertifyMethod.AnnotationsAndFormFilling;
//the signature will be timestamped.
ps.TimeStamping.ServerUrl = new Uri("https://ca.signfiles.com/TSAServer.aspx");
ps.TimeStamping.UserName = "your_username";
ps.TimeStamping.Password = "your_password";
//add signature rectangle to all pages
ps.SignatureAppearsOnAllPages = true;
//write the signed file
File.WriteAllBytes(signedDocument, ps.ApplyDigitalSignature());
}
See also: