Sample code for 30+ languages & platforms
Java

Get PDF Signer Certs

See more PDF Signatures Examples

This example demonstrates how to validate the signatures in a PDF and also shows how to get

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Pdf pdf = new Pdf();

        // Load a PDF that has cryptographic signatures to be validated
        try {
            pdf.loadFile("qa_data/pdf/sign_testing_1/helloSigned2.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Each time we verify a signature, information about the signature is written into
        // sigInfo (replacing whatever sigInfo previously contained).
        JsonObject sigInfo = new JsonObject();

        // Iterate over each signature and validate each.
        int numSignatures = pdf.getNumSignatures();
        boolean validated = false;
        Cert cert = new Cert();
        int i = 0;
        while (i < numSignatures) {
            try {
                pdf.verifySignature(i, sigInfo);
                validated = true;
            } catch (ChilkatException e) {
                validated = false;
            }
            System.out.println("Signature " + i + " validated: " + validated);

            // After calling VerifySignature, you can get the signer certificate by calling
            // GetSignerCert with the same index.
            try {
                pdf.getSignerCert(i, cert);
                success = true;
            } catch (ChilkatException e) {
                success = false;
            }
            if (success) {
                System.out.println("PDF signer certificate: " + cert.getSubjectDN());
            }

            i = i + 1;
        }

        System.out.println("Finished.");
    }
}