Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Find Out Whether a PDF Needs a Password Before Loading It

See more PDF Protection Examples

Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The PDF used in this example is available at two_page_memo.pdf

        // The PDF used in this example is available at encrypted_user_password.pdf

        // The PDF used in this example is available at no_copy_permission.pdf

        Pdf pdf = new Pdf();
        JsonObject info = new JsonObject();

        // A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
        try {
            pdf.preLoadCheck("qa_data/pdf/two_page_memo.pdf", info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("two_page_memo.pdf");
        System.out.println("  encrypted: " + info.boolOf("encrypted"));

        // A PDF that needs a password to open.
        try {
            pdf.preLoadCheck("qa_data/pdf/encrypted_user_password.pdf", info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("encrypted_user_password.pdf");
        System.out.println("  encrypted: " + info.boolOf("encrypted"));
        System.out.println("  method: " + info.stringOf("method"));
        System.out.println("  algorithm: " + info.stringOf("algorithm"));
        System.out.println("  openPasswordRequired: " + info.boolOf("openPasswordRequired"));

        // A PDF that opens without a password but is restricted.  The permissions are readable without
        // any password.
        try {
            pdf.preLoadCheck("qa_data/pdf/no_copy_permission.pdf", info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("no_copy_permission.pdf");
        System.out.println("  encrypted: " + info.boolOf("encrypted"));
        System.out.println("  openPasswordRequired: " + info.boolOf("openPasswordRequired"));
        System.out.println("  may copy: " + info.boolOf("permissions.copy"));

        // The typical use: ask for a password only when one is needed, then load.
        try {
            pdf.preLoadCheck("qa_data/pdf/encrypted_user_password.pdf", info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (info.boolOf("openPasswordRequired")) {
            // An application would prompt for the password here.
            pdf.setUserPassword("secret");
        }

        try {
            pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("loaded, pages: " + pdf.getNumPages());

        // Sample output:

        // two_page_memo.pdf
        //   encrypted: False
        // encrypted_user_password.pdf
        //   encrypted: True
        //   method: password
        //   algorithm: rc4-128
        //   openPasswordRequired: True
        // no_copy_permission.pdf
        //   encrypted: True
        //   openPasswordRequired: False
        //   may copy: False
        // loaded, pages: 1
    }
}