Created
February 25, 2020 16:17
-
-
Save aspose-com-gists/03c3ca31623750822d7c80438a76d786 to your computer and use it in GitHub Desktop.
Digitally Sign PDF Documents in .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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