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

Extract One Section of a PDF by its Heading

See more PDF Text Extraction Examples

Demonstrates "labelValue": "section", which returns everything from a heading up to the next heading of the same or higher level. With the "markdown" operation the section keeps its own sub-headings.

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 sectioned_doc.pdf

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

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

    //  The document has the sections Introduction, Opening Hours, Terms and Conditions (with the
    //  sub-sections Admission, Safety and Liability) and Contact.  Get the Terms and Conditions:
    //  everything after that heading up to the next heading of the same level (Contact).  The heading
    //  itself is not part of the value; labelMatches[0].label has it.
    json.UpdateString("label","Terms and Conditions");
    json.UpdateString("labelValue","section");

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

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

    //  The same with the "text" operation gives the section as plain text.
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    //  Sample output:

    //  ### Admission
    //  
    //  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    //  
    //  ### Safety
    //  
    //  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    //  
    //  ### Liability
    //  
    //  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
    //  ---- as text ----
    //  Admission
    //  
    //  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    //  
    //  Safety
    //  
    //  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    //  
    //  Liability
    //  
    //  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.

  }

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