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

Extract Text from Specific PDF Pages

See more PDF Text Extraction Examples

Demonstrates the "pages" option, which selects the pages to extract (a list of pages and ranges, or "first", "last", "odd", "even"), the "pageSeparator" option, and how to get the text of each page separately.

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 four-page PDF used in this example is available at four_pages.pdf

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

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

    //  Pages are numbered from 1.  The "pages" option can be "all" (the default), "first", "last",
    //  "odd", "even", or a list such as "1,3,5-9,12-".  Here we extract pages 2 and 3.
    json.UpdateString("pages","2-3");

    //  By default the pages in the joined text are separated by a form feed character.
    //  Use any string instead:
    json.UpdateString("pageSeparator","\n----- next page -----\n");

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

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

    //  The "pages" array has the text of each extracted page with its page number.
    //  This is the way to keep the text of each page separate.
    json.UpdateString("pages","1,4");
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    int i = 0;
    int count_i = jsonOut.SizeOfArray("pages");
    while (i < count_i) {
        jsonOut.put_I(i);
        Log.i(TAG, "Page " + String.valueOf(jsonOut.IntOf("pages[i].page")) + ": " + jsonOut.stringOf("pages[i].text"));
        i = i + 1;
        }

    //  Sample output:

    //  Chapter Two: The Crossing
    //  
    //  This is page two of four. The crossing takes forty minutes in calm weather. Today the sea was flat and the gulls followed the boat the whole way.
    //  
    //  ----- next page -----
    //  Chapter Three: Arrival
    //  
    //  This is page three of four. The island harbor was busy with the morning catch being unloaded. The potatoes went straight up the hill to the store on a hand cart.
    //  
    //  
    //  Page 1: Chapter One: Departure
    //  
    //  This is page one of four. The ferry left the north pier at seven in the morning with eleven passengers, two dogs and a crate of seed potatoes for the island store.
    //  
    //  Page 4: Chapter Four: Return
    //  
    //  This is page four of four. The ferry turned round within the hour and sailed back with the mail, three passengers and an empty crate.

  }

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