Android™ Requires Chilkat v11.7.0+
Android™
Get Every Occurrence of a Label in a PDF
See more PDF Text Extraction Examples
Demonstrates the "labelOccurrence" option. When the same label appears more than once (for example "Total:" on each of several receipts), "first" (the default) uses the first occurrence, "all" returns every occurrence with its page, and a number selects one occurrence.Chilkat Android™ Downloads
// 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 (three receipts, one per page) is available at three_receipts.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/three_receipts.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
// Each of the three pages is a receipt with a "Total:" line.
json.UpdateString("label","Total");
json.UpdateString("labelValue","right");
// By default only the first occurrence (in reading order) is used.
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "First total: " + jsonOut.stringOf("text"));
// labelOccurrence "all" returns every occurrence. The "text" member holds the values joined
// by line breaks, and labelMatches has one entry per occurrence with its page.
json.UpdateString("labelOccurrence","all");
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
int i = 0;
int count_i = jsonOut.SizeOfArray("labelMatches");
while (i < count_i) {
jsonOut.put_I(i);
Log.i(TAG, "Page " + String.valueOf(jsonOut.IntOf("labelMatches[i].page")) + " total: " + jsonOut.stringOf("labelMatches[i].text"));
i = i + 1;
}
// A number selects one occurrence: 2 is the second.
json.UpdateInt("labelOccurrence",2);
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Second total: " + jsonOut.stringOf("text"));
// The occurrences are counted over the selected pages, so combining "pages" with the label
// is another way to pick one receipt:
json.UpdateString("labelOccurrence","first");
json.UpdateString("pages","3");
json.UpdateString("label","Receipt No.");
success = pdf.ExtractText("text",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Receipt number on page 3: " + jsonOut.stringOf("text"));
// Sample output:
// First total: $19.86
// Page 1 total: $19.86
// Page 2 total: $17.34
// Page 3 total: $23.62
// Second total: $17.34
// Receipt number on page 3: 1003
}
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."
}
}