Android™ Requires Chilkat v11.7.0+
Android™
Extract a PDF Table with Merged Cells
See more PDF Text Extraction Examples
Demonstrates how merged cells are reported by the "tables" operation: a header cell that spans several columns has colSpan greater than 1, and a row label that spans several rows has rowSpan greater than 1. Also shows the minRows and minColumns options.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 is available at spans_table.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/spans_table.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
// Grids smaller than minRows x minColumns are not reported as tables. The defaults are 2 and 2.
// Setting them higher skips small boxes that are not really tables.
json.UpdateInt("minRows",3);
json.UpdateInt("minColumns",3);
success = pdf.ExtractText("tables",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Tables found: " + String.valueOf(jsonOut.IntOf("count")));
Log.i(TAG, String.valueOf(jsonOut.IntOf("tables[0].rows")) + " rows x " + String.valueOf(jsonOut.IntOf("tables[0].columns")) + " columns");
// A merged cell is reported once, at its top-left position, with its spans.
// The positions it covers have no cell of their own.
int rowSpan;
int colSpan;
int j = 0;
int count_j = jsonOut.SizeOfArray("tables[0].cells");
while (j < count_j) {
jsonOut.put_J(j);
rowSpan = jsonOut.IntOf("tables[0].cells[j].rowSpan");
colSpan = jsonOut.IntOf("tables[0].cells[j].colSpan");
if ((rowSpan > 1) or (colSpan > 1)) {
Log.i(TAG, "row " + String.valueOf(jsonOut.IntOf("tables[0].cells[j].row")) + ", col " + String.valueOf(jsonOut.IntOf("tables[0].cells[j].col")) + ": \""
+ jsonOut.stringOf("tables[0].cells[j].text") + "\" spans " + String.valueOf(rowSpan) + " row(s) and " + String.valueOf(colSpan) + " column(s)");
}
j = j + 1;
}
// Sample output:
// Tables found: 1
// 6 rows x 6 columns
// row 0, col 0: "Area" spans 2 row(s) and 1 column(s)
// row 0, col 1: "City" spans 2 row(s) and 1 column(s)
// row 0, col 2: "First Half" spans 1 row(s) and 2 column(s)
// row 0, col 4: "Second Half" spans 1 row(s) and 2 column(s)
// row 2, col 0: "East" spans 2 row(s) and 1 column(s)
// row 4, col 0: "West" spans 2 row(s) and 1 column(s)
}
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."
}
}