Sample code for 30+ languages & platforms
Lazarus Pascal 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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Pdf,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  j: Integer;
  count_j: Integer;
  i: Integer;
  count_i: Integer;

begin
  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

  pdf := TPdf.Create;
  success := pdf.LoadFile('qa_data/pdf/mixed_text_image.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;

  //  "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) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  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.
  WriteLn('Rating: ' + jsonOut.StringOf('quality.rating'));

  //  Is the PDF tagged (does it have a structure tree)?
  WriteLn('Tagged: ' + jsonOut.BoolOf('quality.tagged'));

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

  //  The per-page report.

  i := 0;
  count_i := jsonOut.SizeOfArray('quality.pages');
  while i < count_i do
    begin
      jsonOut.I := i;
      //  textSource is "text" (real text), "ocrLayer" (invisible text over an image),
      //  "imageOnly" (needs OCR), "mixed", or "empty".
      WriteLn('Page ' + 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 do
        begin
          jsonOut.J := j;
          WriteLn('    ' + jsonOut.StringOf('quality.pages[i].issues[j].code') + ': ' + jsonOut.StringOf('quality.pages[i].issues[j].message'));
          j := j + 1;
        end;

      i := i + 1;
    end;

  //  Decide what to do with the document:
  if (jsonOut.StringOf('quality.rating') = 'none') then
    begin
      WriteLn('No extractable text; the whole document needs OCR.');
    end
  else
    begin
      if (jsonOut.StringOf('quality.pagesNeedingOcr') <> '') then
        begin
          WriteLn('Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.');
        end
      else
        begin
          WriteLn('All pages can be extracted.');
        end;
    end;

  //  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.


  pdf.Free;
  json.Free;
  jsonOut.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.