Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/0d8ebf3a10027842cebb1b6aa775520a to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0d8ebf3a10027842cebb1b6aa775520a to your computer and use it in GitHub Desktop.
Digitally Sign PDF using Java
// 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");
// 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");
// 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