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

Extract Text from a PDF that Forbids Copying

See more PDF Text Extraction Examples

Demonstrates a PDF that opens without a password but whose permissions do not allow copying text. The "text", "markdown", "search" and "tables" operations fail with that reason; "assess" always works. Loading the PDF with the OwnerPassword property set grants owner rights, and extraction works.

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 (owner password: owner-secret; copying not permitted) is available at no_copy_permission.pdf

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

    //  The PDF opens without a password.
    success = pdf.LoadFile("qa_data/pdf/no_copy_permission.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  Extracting text is refused, because the PDF's permissions do not allow copying.
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, "Extraction refused: " + pdf.lastErrorText());
        }

    //  "assess" reports only counts and ratings, never text, so it is always allowed.
    success = pdf.ExtractText("assess",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "rating: " + jsonOut.stringOf("quality.rating"));

    //  Load with the owner password to get owner rights, which include copying.
    pdf.put_OwnerPassword("owner-secret");
    success = pdf.LoadFile("qa_data/pdf/no_copy_permission.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    Log.i(TAG, jsonOut.stringOf("text"));

    //  Sample output:

    //  Extraction refused: ChilkatLog:
    //    ExtractText:
    //      ...
    //        The PDF's permissions do not allow copying or extracting text (load it with the owner password to extract).
    //      ...
    //  rating: good
    //  Visitor Leaflet
    //  
    //  Welcome to the harbor lighthouse. The tower is open on weekends from July to September.
    //  
    //  Please keep to the marked paths and do not climb on the breakwater.
    //  
    //  The lantern room is closed in high winds.

  }

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