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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

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

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();

    //  Opened with the user password.
    CkPdf user = new CkPdf();
    user.put_UserPassword("secret");
    success = user.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
    if (success == false) {
        Log.i(TAG, user.lastErrorText());
        return;
        }

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

    Log.i(TAG, "opened with: " + jsonOut.stringOf("loaded.openedWith"));

    //  Changing the security needs owner rights, so this is refused.
    success = user.Protection("removeEncryption",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "removeEncryption was refused: the PDF was not opened with its owner password.");
        }

    //  Opened with the owner password.
    CkPdf owner = new CkPdf();
    owner.put_OwnerPassword("owner-secret");
    success = owner.LoadFile("qa_data/pdf/encrypted_user_password.pdf");
    if (success == false) {
        Log.i(TAG, owner.lastErrorText());
        return;
        }

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

    Log.i(TAG, "opened with: " + jsonOut.stringOf("loaded.openedWith"));

    //  Now it is allowed.
    success = owner.Protection("removeEncryption",json,jsonOut);
    if (success == false) {
        Log.i(TAG, owner.lastErrorText());
        return;
        }

    Log.i(TAG, "removeEncryption succeeded; will be saved encrypted: " + String.valueOf(jsonOut.BoolOf("pending.encrypted")));

    //  Sample output:

    //  opened with: user
    //  removeEncryption was refused: the PDF was not opened with its owner password.
    //  opened with: owner
    //  removeEncryption succeeded; will be saved encrypted: 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."
  }
}