Android™ Requires Chilkat v11.7.0+
Android™
Extract PDF Text to a StringBuilder and Save to a File
See more PDF Text Extraction Examples
Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.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 long_report.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/long_report.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Number of pages: " + String.valueOf(pdf.get_NumPages()));
// ExtractTextSb takes the same operation and options as ExtractText, but the text
// goes to a StringBuilder. This avoids building a very large JSON string for
// documents with many pages.
CkJsonObject json = new CkJsonObject();
CkStringBuilder sbText = new CkStringBuilder();
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.ExtractTextSb("text",json,sbText,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Extracted " + String.valueOf(sbText.get_Length()) + " characters.");
// Save the text to a file.
success = sbText.WriteFile("qa_output/long_report.txt","utf-8",false);
if (success == false) {
Log.i(TAG, sbText.lastErrorText());
return;
}
// The JSON output does not contain the text, but it still has the quality report.
Log.i(TAG, "Quality: " + jsonOut.stringOf("quality.rating"));
Log.i(TAG, "Pages with no text (need OCR): [" + jsonOut.stringOf("quality.pagesNeedingOcr") + "]");
// Sample output:
// Number of pages: 3
// Extracted 4522 characters.
// Quality: good
// Pages with no text (need OCR): []
}
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."
}
}