Android™ Requires Chilkat v11.7.0+
Android™
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.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();
// Removing the encryption needs owner rights: load with the owner password.
pdf.put_OwnerPassword("owner-secret");
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.Protection("getInfo",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "encrypted, as loaded: " + String.valueOf(jsonOut.BoolOf("loaded.encrypted")));
Log.i(TAG, "algorithm: " + jsonOut.stringOf("loaded.algorithm"));
success = pdf.Protection("removeEncryption",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "will be saved encrypted: " + String.valueOf(jsonOut.BoolOf("pending.encrypted")));
success = pdf.WriteFile("qa_output/minutes_unprotected.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// The result is an ordinary PDF that opens without a password.
CkPdf reader = new CkPdf();
CkJsonObject info = new CkJsonObject();
success = reader.PreLoadCheck("qa_output/minutes_unprotected.pdf",info);
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
Log.i(TAG, "encrypted: " + String.valueOf(info.BoolOf("encrypted")));
success = reader.LoadFile("qa_output/minutes_unprotected.pdf");
if (success == false) {
Log.i(TAG, reader.lastErrorText());
return;
}
Log.i(TAG, "pages: " + String.valueOf(reader.get_NumPages()));
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// 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."
}
}