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

Exclude or Include Running Headers and Page Numbers

See more PDF Text Extraction Examples

Demonstrates the "repeatedHeadersFooters" filter. Text repeated at the same position on most pages, such as a running header and "Page N of 4" footers, is left out by default; set the filter to "include" to keep it.

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

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

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

    //  Every page has the header "Harbor Lighthouse Society - Annual Report 2026" at the top right
    //  and "Page N of 4" at the bottom.  By default both are excluded, so the text of page 2 is just
    //  its heading and paragraphs.
    json.UpdateString("pages","2");
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "---- default (repeated headers and footers excluded) ----");
    Log.i(TAG, jsonOut.stringOf("text"));

    //  Include them.  The detection looks at the neighbouring pages even when only one page is
    //  selected, so a single page extracted on its own still loses its running head by default.
    json.UpdateString("repeatedHeadersFooters","include");
    success = pdf.ExtractText("text",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "---- repeatedHeadersFooters: include ----");
    Log.i(TAG, jsonOut.stringOf("text"));

    //  Sample output:

    //  ---- default (repeated headers and footers excluded) ----
    //  The Work
    //  
    //  The restoration society took ownership in 2021 with three goals: make the structure watertight, return the tower to public access, and relight the lantern with a historically accurate lens. The first two were met in the second season, and the lens arrived on loan in November.
    //  
    //  The spiral stair is original and is the most fragile part of the building. Twelve of the ninety-one treads were cracked or missing, and the central column had corroded badly at the second landing. The treads were cast from patterns taken off an intact tread.
    //  ---- repeatedHeadersFooters: include ----
    //  Harbor Lighthouse Society - Annual Report 2026
    //  
    //  The Work
    //  
    //  The restoration society took ownership in 2021 with three goals: ...
    //  
    //  The spiral stair is original and is the most fragile part of the building. ...
    //  
    //  Page 2 of 4

  }

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