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

Get the Encryption and Permissions of a Loaded PDF

See more PDF Protection Examples

Demonstrates the getInfo operation of Protection, which reports how the loaded PDF is protected: the method and algorithm, whether a password is needed to open it, how it was opened (with the user password, the owner password, an empty password, or a certificate), and each permission. The loaded member describes the file as it is, the pending member describes the security it will be saved with, and changed says whether the two differ. getInfo is the one operation that does not need owner rights.

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;
        }

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();
    success = pdf.Protection("getInfo",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    jsonOut.put_EmitCompact(false);
    Log.i(TAG, jsonOut.emit());

    //  "loaded" is the security the file has.  "pending" is the security it will be saved with, which
    //  is the same until an operation such as encrypt or changePermissions changes it.
    Log.i(TAG, "algorithm: " + jsonOut.stringOf("loaded.algorithm"));
    Log.i(TAG, "opened with: " + jsonOut.stringOf("loaded.openedWith"));
    Log.i(TAG, "password needed to open: " + String.valueOf(jsonOut.BoolOf("loaded.userPasswordRequired")));
    Log.i(TAG, "may print: " + jsonOut.stringOf("loaded.permissions.print"));
    Log.i(TAG, "may copy: " + String.valueOf(jsonOut.BoolOf("loaded.permissions.copy")));
    Log.i(TAG, "changed: " + String.valueOf(jsonOut.BoolOf("changed")));

    //  Sample output:

    //  {
    //    "changed": false,
    //    "loaded": {
    //      "encrypted": true,
    //      "method": "password",
    //      "algorithm": "rc4-128",
    //      "pdfVersion": "1.3",
    //      "scope": "all",
    //      "userPasswordRequired": true,
    //      "ownerPasswordSet": true,
    //      "openedWith": "user",
    //      "permissions": {
    //        "print": "full",
    //        "modify": true,
    //        "copy": true,
    //        "accessibility": true,
    //        "annotate": true,
    //        "fillForms": true,
    //        "assemble": true
    //      }
    //    },
    //    "pending": {
    //      "encrypted": true,
    //      "method": "password",
    //      "algorithm": "rc4-128",
    //      "pdfVersion": "1.3",
    //      "scope": "all",
    //      "userPasswordRequired": true,
    //      "ownerPasswordSet": true,
    //      "permissions": {
    //        "print": "full",
    //        "modify": true,
    //        "copy": true,
    //        "accessibility": true,
    //        "annotate": true,
    //        "fillForms": true,
    //        "assemble": true
    //      }
    //    }
    //  }
    //  
    //  algorithm: rc4-128
    //  opened with: user
    //  password needed to open: True
    //  may print: full
    //  may copy: True
    //  changed: 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."
  }
}