Created
July 16, 2020 16:34
-
-
Save aspose-com-gists/0d8ebf3a10027842cebb1b6aa775520a to your computer and use it in GitHub Desktop.
Digitally Sign PDF using Java
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
// Create a Document object | |
Document doc = new Document("input.pdf"); | |
PdfFileSignature signature = new PdfFileSignature(doc); | |
PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objects | |
TimestampSettings timestampSettings = new TimestampSettings("https:\\your_timestamp_settings", "user:password"); // User/Password can be omitted | |
pkcs.setTimestampSettings(timestampSettings); | |
Rectangle rect = new Rectangle(100, 600, 400, 100); | |
// Set signature appearance | |
signature.setSignatureAppearance("aspose-logo.png"); | |
// Create any of the three signature types | |
signature.sign(1, "Signature Reason", "Contact", "Location", true, rect, pkcs); | |
// 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
// Create a Document object | |
Document doc = new Document("input.pdf"); | |
PdfFileSignature signature = new PdfFileSignature(doc); | |
PKCS7 pkcs = new PKCS7("certificate.pfx", "1234567890"); // Use PKCS7/PKCS7Detached objects | |
DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms); | |
Rectangle rect = new Rectangle(100, 600, 400, 100); | |
// Set signature appearance | |
signature.setSignatureAppearance("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
// Create PDF File Signature | |
PdfFileSignature pdfSign = new PdfFileSignature(); | |
// Bind PDF | |
pdfSign.bindPdf("Digitally Signed PDF.pdf"); | |
// Verify signature using signature name | |
if (pdfSign.verifySigned("Signature1")) | |
{ | |
if (pdfSign.isCertified()) // Certified? | |
{ | |
if (pdfSign.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