Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.7.0+

Optimizing an Encrypted PDF

See more PDF Optimization Examples

Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.

Chilkat Android™ Downloads

Android™
// 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();

    //  The password must be set before loading.
    pdf.put_UserPassword("secret");
    success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  What security does the file have, and may it be modified?
    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: " + String.valueOf(jsonOut.BoolOf("loaded.encrypted")));
    Log.i(TAG, "method:    " + jsonOut.stringOf("loaded.method"));
    Log.i(TAG, "openedWith: " + jsonOut.stringOf("loaded.openedWith"));
    Log.i(TAG, "may modify: " + String.valueOf(jsonOut.BoolOf("loaded.permissions.modify")));

    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    success = pdf.WriteFile("qa_output/encrypted_optimized.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "bytesSaved: " + String.valueOf(jsonOut.IntOf("bytesSaved")));

    //  The output is still encrypted with the same security: loading it without the password fails.
    CkPdf noPassword = new CkPdf();
    success = noPassword.LoadFile("qa_output/encrypted_optimized.pdf");
    CkJsonObject q = new CkJsonObject();
    CkJsonObject qOut = new CkJsonObject();
    success = noPassword.ExtractText("assess",q,qOut);
    Log.i(TAG, "passwordRequired without the password: " + String.valueOf(qOut.BoolOf("quality.passwordRequired")));

    //  With the password it reads normally.
    CkPdf withPassword = new CkPdf();
    withPassword.put_UserPassword("secret");
    success = withPassword.LoadFile("qa_output/encrypted_optimized.pdf");
    if (success == false) {
        Log.i(TAG, withPassword.lastErrorText());
        return;
        }

    Log.i(TAG, "pages: " + String.valueOf(withPassword.get_NumPages()));

  }

  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."
  }
}