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

Get the Text Block Below a PDF Caption

See more PDF Text Extraction Examples

Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.

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 packing slip used in this example is available at packing_slip.pdf

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

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

    //  The packing slip has the captions "Bill To" and "Ship To" side by side,
    //  each with an address block below it.
    json.UpdateString("label","Ship To");

    //  "below" means the value is the block of text directly under the label.
    json.UpdateString("labelValue","below");

    //  With the default "flow" layout the lines of the block would be joined into one paragraph.
    //  "lines" keeps each line of the address on its own line.
    json.UpdateString("layout","lines");

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

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

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

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

    //  A caption over a short paragraph works the same way.  Here the flow layout is better,
    //  because the two lines are one piece of text.
    json.UpdateString("label","Special Instructions");
    json.UpdateString("layout","flow");
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

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

    //  Sample output:

    //  Ship To:
    //  Frank Delgado
    //  c/o Harbor Pet Boarding
    //  9 Ferry Lane
    //  Portside, WA 98001
    //  
    //  Bill To:
    //  Frank Delgado
    //  Apartment 3B
    //  41 Mill Street
    //  Portside, WA 98000
    //  
    //  Special Instructions:
    //  Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.

  }

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