Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/03c3ca31623750822d7c80438a76d786 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/03c3ca31623750822d7c80438a76d786 to your computer and use it in GitHub Desktop.
Digitally Sign PDF Documents in .NET
// Load PDF document
using (Document pdfDocument = new Document("Document.pdf"))
{
using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
{
PKCS7 pkcs = new PKCS7("certificate.pfx", "WebSales"); // Use PKCS7/PKCS7Detached objects
TimestampSettings timestampSettings = new TimestampSettings("https:\\your_timestamp_settings", "user:password"); // User/Password can be omitted
pkcs.TimestampSettings = timestampSettings;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
// Create any of the three signature types
signature.Sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs);
// Save output PDF file
signature.Save("Output.pdf");
}
}
// Load PDF document
using (Document pdfDocument = new Document("Document.pdf"))
{
using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
{
PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objects
DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
// Set signature appearance
signature.SignatureAppearance = @"aspose-logo.png";
// Create any of the three signature types
signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
// Save digitally signed PDF file
signature.Save("Digitally Signed PDF.pdf");
}
}
// Load PDF document
using (Document pdfDocument = new Document("Document.pdf"))
{
using (PdfFileSignature signature = new PdfFileSignature(pdfDocument))
{
IList<string> sigNames = signature.GetSignNames();
if (sigNames.Count > 0) // Any signatures?
{
if (signature.VerifySigned(sigNames[0] as string)) // Verify first one
{
if (signature.IsCertified) // Certified?
{
if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission
{
// Do something
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment