Extract Signature Information from PDF in Java
Contents
[
Hide
]
Use PdfFileSignature to inspect and manage signatures that already exist in a PDF document.
Read signature information
- Create the PdfFileSignature facade and bind the source PDF document.
- Access the document signature name and configure the signature-inspection flow required by the example.
- Read and verify the signature information from the PdfFileSignature facade.
- Read the returned values or continue with your next processing step.
public static void getSignatureInformation(Path inputFile) {
PdfFileSignature pdfSignature = new PdfFileSignature();
try {
pdfSignature.bindPdf(inputFile.toString());
SignatureName signatureName = pdfSignature.getSignatureNames().get_Item(0);
System.out.println("Signature Names: " + pdfSignature.getSignNames());
System.out.println("Signer: " + pdfSignature.getSignerName(signatureName));
System.out.println("Date: " + pdfSignature.getDateTime(signatureName));
System.out.println("Reason: " + pdfSignature.getReason(signatureName));
System.out.println("Location: " + pdfSignature.getLocation(signatureName));
} finally {
pdfSignature.close();
}
}
Verify a signature
- Create the PdfFileSignature facade and bind the source PDF document.
- Access the document signature name and configure the verification flow required by the example.
- Read and verify the signature information from the PdfFileSignature facade.
public static void verifyPdfSignature(Path inputFile) {
PdfFileSignature pdfSignature = new PdfFileSignature();
try {
pdfSignature.bindPdf(inputFile.toString());
SignatureName signatureName = pdfSignature.getSignatureNames().get_Item(0);
System.out.println("Signature '" + signatureName + "' is valid: "
+ pdfSignature.verifySignature(signatureName));
System.out.println("Signature covers whole document: "
+ pdfSignature.coversWholeDocument(signatureName));
} finally {
pdfSignature.close();
}
}
Extract the signing certificate
- Create the PdfFileSignature facade and bind the source PDF document.
- Access the document signature name required for certificate extraction.
- Write the extracted output or inspect the returned values from the PdfFileSignature facade.
public static void extractSignatureCertificate(Path inputFile, Path outputFile) throws Exception {
PdfFileSignature pdfSignature = new PdfFileSignature();
try {
pdfSignature.bindPdf(inputFile.toString());
SignatureName signatureName = pdfSignature.getSignatureNames().get_Item(0);
try (InputStream inputStream = pdfSignature.extractCertificate(signatureName);
OutputStream outputStream = Files.newOutputStream(outputFile)) {
inputStream.transferTo(outputStream);
}
} finally {
pdfSignature.close();
}
}