Android™ Requires Chilkat v11.7.0+
Android™
Password-Protect a PDF with AES-256 Encryption
See more PDF Protection Examples
Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.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
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The passwords are taken from these two properties at the time Protection is called.
// The user password opens the PDF. The owner password opens it too, and is the one needed
// later to change or remove the protection.
pdf.put_UserPassword("user-secret");
pdf.put_OwnerPassword("owner-secret");
// An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Protection("encrypt",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
Log.i(TAG, "algorithm: " + jsonOut.stringOf("pending.algorithm"));
Log.i(TAG, "PDF version: " + jsonOut.stringOf("pending.pdfVersion"));
Log.i(TAG, "password needed to open: " + String.valueOf(jsonOut.BoolOf("pending.userPasswordRequired")));
// Writing the file applies it.
success = pdf.WriteFile("qa_output/memo_encrypted.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// PreLoadCheck reports a file's encryption without loading it, and needs no password.
CkPdf checker = new CkPdf();
CkJsonObject info = new CkJsonObject();
success = checker.PreLoadCheck("qa_output/memo_encrypted.pdf",info);
if (success == false) {
Log.i(TAG, checker.lastErrorText());
return;
}
Log.i(TAG, "encrypted: " + String.valueOf(info.BoolOf("encrypted")));
Log.i(TAG, "openPasswordRequired: " + String.valueOf(info.BoolOf("openPasswordRequired")));
// With the password, the PDF opens normally.
CkPdf reader = new CkPdf();
reader.put_UserPassword("user-secret");
success = reader.LoadFile("qa_output/memo_encrypted.pdf");
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
Log.i(TAG, "pages: " + String.valueOf(reader.get_NumPages()));
// Sample output:
// algorithm: aes-256
// PDF version: 1.7
// password needed to open: True
// encrypted: True
// openPasswordRequired: True
// pages: 2
}
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."
}
}