Android™ Requires Chilkat v11.7.0+
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// 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
CkPdf pdf = new CkPdf();
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
// The PDF loads without its password: the file's structure is readable, so properties such as
// NumPages and the security settings are available...
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// ...but the content cannot be decrypted, so extracting text fails.
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, "Cannot extract: " + pdf.lastErrorText());
}
// "assess" still works: it reports that a password is required and nothing else.
success = pdf.ExtractText("assess",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "passwordRequired: " + String.valueOf(jsonOut.BoolOf("quality.passwordRequired")));
Log.i(TAG, "rating: " + jsonOut.stringOf("quality.rating"));
// Set the password and load again.
pdf.put_UserPassword("secret");
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, 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.
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}