Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

Extract Text from a Two-Column PDF

See more PDF Text Extraction Examples

Demonstrates how multi-column pages are read. By default the columns are detected and each column is read top to bottom before the next ("columns": "auto"); "columns": "none" reads the full page width line by line, which interleaves the columns. The quality report tells how many columns were found.

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;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  //  The two-column PDF used in this example is available at two_column_article.pdf

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

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

  //  With the default options the columns are detected, and the text of the left column
  //  comes before the text of the right column.
  success := pdf.ExtractText('text',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  WriteLn('Columns detected: ' + jsonOut.IntOf('quality.pages[0].columns'));
  WriteLn('Reading order: ' + jsonOut.StringOf('quality.pages[0].readingOrderSource'));
  WriteLn(jsonOut.StringOf('text'));

  //  "none" switches column detection off: the page is read across its full width, line by line,
  //  so each output line holds a line of the left column followed by a line of the right column.
  //  This is rarely wanted, but it shows what the detection does.
  json.UpdateString('columns','none');
  json.UpdateString('layout','lines');
  success := pdf.ExtractText('text',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  WriteLn('---- columns: none ----');
  WriteLn(jsonOut.StringOf('text'));

  //  Sample output (abbreviated):

  //  Columns detected: 2
  //  Reading order: visual
  //  The Harbor Lighthouse, Season Two
  //  
  //  The harbor lighthouse was built in 1874 to mark the entrance to the inner channel. It operated continuously until 1968, ...
  //  
  //  The restoration society took ownership in 2021 with three goals: ...
  //  ...
  //  The lantern room frame was stripped, primed and repainted in the original dark green. ...
  //  ...
  //  ---- columns: none ----
  //  The Harbor Lighthouse, Season Two
  //  The harbor lighthouse was built in 1874 to The lantern room frame was stripped, primed
  //  mark the entrance to the inner channel. It and repainted in the original dark green. New
  //  ...


  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.