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

Assess PDF Text Quality Before Extracting

See more PDF Text Extraction Examples

Demonstrates the "assess" operation, which returns only the quality report: whether the text can be trusted, which pages are scanned images that need OCR, and how each page's text was found. Use it to triage documents before extracting. (Chilkat does not perform OCR.)

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 (page 2 is a scanned image with no text) is available at mixed_text_image.pdf

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

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

    //  "assess" computes the quality report without producing any text.
    //  It is fast, and it is always allowed, even for an encrypted PDF that forbids copying.
    success = pdf.ExtractText("assess",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  The overall rating is the worst rating among the pages that have text:
    //  "good", "fair", "poor", or "none" when there is no text at all.
    Log.i(TAG, "Rating: " + jsonOut.stringOf("quality.rating"));

    //  Is the PDF tagged (does it have a structure tree)?
    Log.i(TAG, "Tagged: " + String.valueOf(jsonOut.BoolOf("quality.tagged")));

    //  The pages that are images only and would need OCR, such as "2" or "4,9".
    Log.i(TAG, "Pages needing OCR: " + jsonOut.stringOf("quality.pagesNeedingOcr"));

    //  The per-page report.
    int j;
    int count_j;
    int i = 0;
    int count_i = jsonOut.SizeOfArray("quality.pages");
    while (i < count_i) {
        jsonOut.put_I(i);
        //  textSource is "text" (real text), "ocrLayer" (invisible text over an image),
        //  "imageOnly" (needs OCR), "mixed", or "empty".
        Log.i(TAG, "Page " + String.valueOf(jsonOut.IntOf("quality.pages[i].page")) + ": rating " + jsonOut.stringOf("quality.pages[i].rating") + ", textSource "
             + jsonOut.stringOf("quality.pages[i].textSource"));
        //  Each issue has a machine-readable code and a sentence.
        j = 0;
        count_j = jsonOut.SizeOfArray("quality.pages[i].issues");
        while (j < count_j) {
            jsonOut.put_J(j);
            Log.i(TAG, "    " + jsonOut.stringOf("quality.pages[i].issues[j].code") + ": " + jsonOut.stringOf("quality.pages[i].issues[j].message"));
            j = j + 1;
            }

        i = i + 1;
        }

    //  Decide what to do with the document:
    if (jsonOut.stringOf("quality.rating") == "none") {
        Log.i(TAG, "No extractable text; the whole document needs OCR.");
        }
    else {
        if (jsonOut.stringOf("quality.pagesNeedingOcr") != "") {
            Log.i(TAG, "Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.");
            }
        else {
            Log.i(TAG, "All pages can be extracted.");
            }

        }

    //  Sample output:

    //  Rating: good
    //  Tagged: false
    //  Pages needing OCR: 2
    //  Page 1: rating good, textSource text
    //  Page 2: rating none, textSource imageOnly
    //      imageOnly: No text; page is a scanned image
    //  Page 3: rating good, textSource text
    //  Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.

  }

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