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

Search a PDF with a Regular Expression

See more PDF Text Extraction Examples

Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).

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

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

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

    //  Find every order number.  With regex set to true, the query is a regular expression.
    json.UpdateString("query","ORD-[0-9]{4}-[0-9]{4}");
    json.UpdateBool("regex",true);
    //  We only want the matched text, so no context is needed.
    json.UpdateInt("context",0);

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

    Log.i(TAG, "Order numbers found: " + String.valueOf(jsonOut.IntOf("count")));
    int i = 0;
    int count_i = jsonOut.SizeOfArray("matches");
    while (i < count_i) {
        jsonOut.put_I(i);
        Log.i(TAG, jsonOut.stringOf("matches[i].text"));
        i = i + 1;
        }

    //  Find the e-mail addresses, but stop after the first three.
    json.UpdateString("query","[a-z0-9._]+@[a-z]+[.][a-z]+");
    json.UpdateInt("maxResults",3);
    success = pdf.ExtractText("search",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "First three e-mail addresses:");
    i = 0;
    count_i = jsonOut.SizeOfArray("matches");
    while (i < count_i) {
        jsonOut.put_I(i);
        Log.i(TAG, jsonOut.stringOf("matches[i].text"));
        i = i + 1;
        }

    //  wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
    //  but it is also inside "catalog" and "category".
    json.Delete("maxResults");
    json.UpdateBool("regex",false);
    json.UpdateString("query","cat");
    success = pdf.ExtractText("search",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "Occurrences of cat (any): " + String.valueOf(jsonOut.IntOf("count")));

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

    Log.i(TAG, "Occurrences of cat (whole word): " + String.valueOf(jsonOut.IntOf("count")));

    //  Sample output:

    //  Order numbers found: 9
    //  ORD-2026-0301
    //  ORD-2026-0302
    //  ORD-2026-0303
    //  ORD-2026-0304
    //  ORD-2026-0305
    //  ORD-2026-0306
    //  ORD-2026-0307
    //  ORD-2026-0308
    //  ORD-2026-0307
    //  First three e-mail addresses:
    //  alice.moore@example.com
    //  bkumar@example.org
    //  carla@example.net
    //  Occurrences of cat (any): 3
    //  Occurrences of cat (whole word): 1

  }

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