Android™ Requires Chilkat v11.7.0+
Android™
Change the Passwords of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.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 encrypted_user_password.pdf
CkPdf pdf = new CkPdf();
// Load with the current passwords.
pdf.put_UserPassword("secret");
pdf.put_OwnerPassword("owner-secret");
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The new passwords. Protection reads the properties as they are when it is called.
pdf.put_UserPassword("new-user-secret");
pdf.put_OwnerPassword("new-owner-secret");
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Protection("changePasswords",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The algorithm and the permissions are kept.
Log.i(TAG, "algorithm: " + jsonOut.stringOf("pending.algorithm"));
Log.i(TAG, "password needed to open: " + String.valueOf(jsonOut.BoolOf("pending.userPasswordRequired")));
success = pdf.WriteFile("qa_output/minutes_new_passwords.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The new user password opens the result.
CkPdf reader = new CkPdf();
reader.put_UserPassword("new-user-secret");
success = reader.LoadFile("qa_output/minutes_new_passwords.pdf");
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
success = reader.Protection("getInfo",json,jsonOut);
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
Log.i(TAG, "with the new user password, opened with: " + jsonOut.stringOf("loaded.openedWith"));
// And so does the new owner password.
CkPdf owner = new CkPdf();
owner.put_OwnerPassword("new-owner-secret");
success = owner.LoadFile("qa_output/minutes_new_passwords.pdf");
if (success == false) {
Log.i(TAG, owner.lastErrorText());
return;
}
success = owner.Protection("getInfo",json,jsonOut);
if (success == false) {
Log.i(TAG, owner.lastErrorText());
return;
}
Log.i(TAG, "with the new owner password, opened with: " + jsonOut.stringOf("loaded.openedWith"));
// Sample output:
// algorithm: rc4-128
// password needed to open: True
// with the new user password, opened with: user
// with the new owner password, opened with: owner
}
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."
}
}