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

Restrict Printing, Copying and Editing of a PDF

See more PDF Protection Examples

Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have 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 two_page_memo.pdf

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  No user password: the PDF opens without a prompt.  The owner password is what protects the
    //  restrictions, so it must not be empty when anything is restricted.
    pdf.put_UserPassword("");
    pdf.put_OwnerPassword("owner-secret");

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("permissions.print","lowQuality");
    json.UpdateBool("permissions.copy",false);
    json.UpdateBool("permissions.modify",false);
    json.UpdateBool("permissions.annotate",false);
    //  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

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

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

    //  Anyone can open the result, with the rights of a user.
    CkPdf reader = new CkPdf();
    success = reader.LoadFile("qa_output/memo_restricted.pdf");
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    CkJsonObject empty = new CkJsonObject();
    CkJsonObject info = new CkJsonObject();
    success = reader.Protection("getInfo",empty,info);
    if (success == false) {
        Log.i(TAG, reader.lastErrorText());
        return;
        }

    Log.i(TAG, "opened with: " + info.stringOf("loaded.openedWith"));
    Log.i(TAG, "print: " + info.stringOf("loaded.permissions.print"));
    Log.i(TAG, "copy: " + String.valueOf(info.BoolOf("loaded.permissions.copy")));
    Log.i(TAG, "modify: " + String.valueOf(info.BoolOf("loaded.permissions.modify")));

    //  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
    CkJsonObject opts = new CkJsonObject();
    CkJsonObject extractOut = new CkJsonObject();
    CkStringBuilder sb = new CkStringBuilder();
    success = reader.ExtractTextSb("text",opts,sb,extractOut);
    if (success == false) {
        Log.i(TAG, "Text extraction was refused: the PDF does not permit copying.");
        }

    //  The owner password lifts every restriction.
    CkPdf owner = new CkPdf();
    owner.put_OwnerPassword("owner-secret");
    success = owner.LoadFile("qa_output/memo_restricted.pdf");
    if (success == false) {
        Log.i(TAG, owner.lastErrorText());
        return;
        }

    CkJsonObject ownerInfo = new CkJsonObject();
    success = owner.Protection("getInfo",empty,ownerInfo);
    if (success == false) {
        Log.i(TAG, owner.lastErrorText());
        return;
        }

    Log.i(TAG, "opened with: " + ownerInfo.stringOf("loaded.openedWith"));
    success = owner.ExtractTextSb("text",opts,sb,extractOut);
    if (success == false) {
        Log.i(TAG, owner.lastErrorText());
        return;
        }

    Log.i(TAG, "characters extracted with the owner password: " + String.valueOf(sb.get_Length()));

    //  Sample output:

    //  opened with: emptyPassword
    //  print: lowQuality
    //  copy: False
    //  modify: False
    //  Text extraction was refused: the PDF does not permit copying.
    //  opened with: owner
    //  characters extracted with the owner password: 1048

  }

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