Extract Signature Information from PDF in Java

Use PdfFileSignature to inspect and manage signatures that already exist in a PDF document.

Read signature information

  1. Create the PdfFileSignature facade and bind the source PDF document.
  2. Access the document signature name and configure the signature-inspection flow required by the example.
  3. Read and verify the signature information from the PdfFileSignature facade.
  4. 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

  1. Create the PdfFileSignature facade and bind the source PDF document.
  2. Access the document signature name and configure the verification flow required by the example.
  3. 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

  1. Create the PdfFileSignature facade and bind the source PDF document.
  2. Access the document signature name required for certificate extraction.
  3. 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();
    }
}