Android™ Requires Chilkat v11.7.0+
Android™
Change the Permissions of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.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();
// Changing the permissions needs owner rights. Both passwords are given here, which is what
// an AES-256 PDF requires for this operation.
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;
}
// No printing and no copying. Everything not mentioned is granted.
CkJsonObject json = new CkJsonObject();
json.UpdateString("permissions.print","none");
json.UpdateBool("permissions.copy",false);
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Protection("changePermissions",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "print: " + jsonOut.stringOf("pending.permissions.print"));
Log.i(TAG, "copy: " + String.valueOf(jsonOut.BoolOf("pending.permissions.copy")));
Log.i(TAG, "modify: " + String.valueOf(jsonOut.BoolOf("pending.permissions.modify")));
Log.i(TAG, "password still needed to open: " + String.valueOf(jsonOut.BoolOf("pending.userPasswordRequired")));
success = pdf.WriteFile("qa_output/minutes_no_print.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The same user password opens the result, now with the new permissions.
CkPdf reader = new CkPdf();
reader.put_UserPassword("secret");
success = reader.LoadFile("qa_output/minutes_no_print.pdf");
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
CkJsonObject empty = new CkJsonObject();
CkJsonObject info = new CkJsonObject();
success = reader.Protection("getInfo",empty,info);
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
Log.i(TAG, "saved file, print: " + info.stringOf("loaded.permissions.print"));
Log.i(TAG, "saved file, copy: " + String.valueOf(info.BoolOf("loaded.permissions.copy")));
// Sample output:
// print: none
// copy: False
// modify: True
// password still needed to open: True
// saved file, print: none
// saved file, copy: False
}
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."
}
}