Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) {
        System.out.println(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) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("encrypted: " + jsonOut.BoolOf("loaded.encrypted"));
    System.out.println("method:    " + jsonOut.stringOf("loaded.method"));
    System.out.println("openedWith: " + jsonOut.stringOf("loaded.openedWith"));
    System.out.println("may modify: " + jsonOut.BoolOf("loaded.permissions.modify"));

    success = pdf.Optimize("optimize",json,jsonOut);
    if (success == false) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    success = pdf.WriteFile("qa_output/encrypted_optimized.pdf");
    if (success == false) {
        System.out.println(pdf.lastErrorText());
        return;
        }

    System.out.println("bytesSaved: " + 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);
    System.out.println("passwordRequired without the password: " + 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) {
        System.out.println(withPassword.lastErrorText());
        return;
        }

    System.out.println("pages: " + withPassword.get_NumPages());
  }
}