Android™ Requires Chilkat v11.7.0+
Android™
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 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 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
CkPdf pdf = new CkPdf();
CkJsonObject info = new CkJsonObject();
// A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
success = pdf.PreLoadCheck("qa_data/pdf/two_page_memo.pdf",info);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "two_page_memo.pdf");
Log.i(TAG, " encrypted: " + String.valueOf(info.BoolOf("encrypted")));
// A PDF that needs a password to open.
success = pdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",info);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "encrypted_user_password.pdf");
Log.i(TAG, " encrypted: " + String.valueOf(info.BoolOf("encrypted")));
Log.i(TAG, " method: " + info.stringOf("method"));
Log.i(TAG, " algorithm: " + info.stringOf("algorithm"));
Log.i(TAG, " openPasswordRequired: " + String.valueOf(info.BoolOf("openPasswordRequired")));
// A PDF that opens without a password but is restricted. The permissions are readable without
// any password.
success = pdf.PreLoadCheck("qa_data/pdf/no_copy_permission.pdf",info);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "no_copy_permission.pdf");
Log.i(TAG, " encrypted: " + String.valueOf(info.BoolOf("encrypted")));
Log.i(TAG, " openPasswordRequired: " + String.valueOf(info.BoolOf("openPasswordRequired")));
Log.i(TAG, " may copy: " + String.valueOf(info.BoolOf("permissions.copy")));
// The typical use: ask for a password only when one is needed, then load.
success = pdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",info);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
if (info.BoolOf("openPasswordRequired") == true) {
// An application would prompt for the password here.
pdf.put_UserPassword("secret");
}
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "loaded, pages: " + String.valueOf(pdf.get_NumPages()));
// 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
}
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."
}
}