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

Extract Text from a Password-Protected PDF

See more PDF Text Extraction Examples

Demonstrates extracting text from a PDF that requires a password to open. The PDF loads without the password (its structure can be read), but the "text" operation fails because the content cannot be decrypted; "assess" reports that a password is required. With the UserPassword property set before loading, extraction works normally.

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 (user password: secret) is available at encrypted_user_password.pdf

        Pdf pdf = new Pdf();
        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // The PDF loads without its password: the file's structure is readable, so properties such as
        // NumPages and the security settings are available...
        try {
            pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // ...but the content cannot be decrypted, so extracting text fails.
        try {
            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println("Cannot extract: " + e.getLastErrorText());
        }

        // "assess" still works: it reports that a password is required and nothing else.
        try {
            pdf.extractText("assess", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("passwordRequired: " + jsonOut.boolOf("quality.passwordRequired"));
        System.out.println("rating: " + jsonOut.stringOf("quality.rating"));

        // Set the password and load again.
        pdf.setUserPassword("secret");
        try {
            pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf");

            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(jsonOut.stringOf("text"));

        // Sample output:

        // Cannot extract: ChilkatLog:
        //   ExtractText:
        //     ...
        //       The PDF is encrypted and could not be opened (wrong or missing password); its text cannot be read.
        //     ...
        // passwordRequired: true
        // rating: none
        // Board Minutes (Confidential)
        //
        // The board met on 3 November 2026. Present: the chair, the treasurer and four members. The treasurer reported that spending for the season came in under budget.
        //
        // The meeting closed at 8.45 p.m.
    }
}