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

Extract Tables from a PDF

See more PDF Text Extraction Examples

Demonstrates the "tables" operation, which returns the tables detected on the pages as rows and cells. Each cell has its row and column (counted from 0), its row and column span, and its text.

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.StringBuilder,
  Chilkat.Pdf,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  sbRow: TStringBuilder;
  currentRow: Integer;
  row: Integer;
  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 is available at price_list.pdf

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

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

  //  The "tables" operation returns the tables found on the selected pages.
  //  Tables are recognized by their drawn cell borders.
  success := pdf.ExtractText('tables',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  WriteLn('Number of tables: ' + jsonOut.IntOf('count'));

  //  Used to build each row of output.
  sbRow := TStringBuilder.Create;

  i := 0;
  count_i := jsonOut.SizeOfArray('tables');
  while i < count_i do
    begin
      jsonOut.I := i;
      WriteLn('Table on page ' + jsonOut.IntOf('tables[i].page') + ': ' + jsonOut.IntOf('tables[i].rows') + ' rows x ' + jsonOut.IntOf('tables[i].columns') + ' columns, detected by '
           + jsonOut.StringOf('tables[i].detectedBy'));

      //  The cells are listed row by row.  Print each row as its cells separated by " | ".
      currentRow := -1;
      sbRow.SetString('');
      j := 0;
      count_j := jsonOut.SizeOfArray('tables[i].cells');
      while j < count_j do
        begin
          jsonOut.J := j;
          row := jsonOut.IntOf('tables[i].cells[j].row');
          if (row <> currentRow) then
            begin
              if (currentRow >= 0) then
                begin
                  WriteLn(sbRow.GetAsString());
                end;
              currentRow := row;
              sbRow.SetString('  ');
            end
          else
            begin
              sbRow.Append(' | ');
            end;
          sbRow.Append(jsonOut.StringOf('tables[i].cells[j].text'));
          j := j + 1;
        end;

      WriteLn(sbRow.GetAsString());
      i := i + 1;
    end;

  //  The cells of the first table as JSON:
  jsonOut.EmitCompact := False;
  WriteLn(jsonOut.Emit());

  //  Sample output:

  //  Number of tables: 1
  //  Table on page 1: 8 rows x 5 columns, detected by ruled
  //    Code | Product | Unit | Price | In Stock
  //    HT-101 | Claw hammer, 16 oz | each | $18.00 | 24
  //    HT-102 | Adjustable wrench, 10 in | each | $14.50 | 31
  //    HT-103 | Screwdriver set, 6 piece | set | $22.00 | 12
  //    HT-104 | Tape measure, 25 ft | each | $9.75 | 40
  //    HT-105 | Utility knife | each | $6.20 | 58
  //    HT-106 | Pliers, needle nose | each | $11.00 | 19
  //    HT-107 | Hand saw, 20 in | each | $21.40 | 8
  //  {
  //    "count": 1,
  //    "tables": [
  //      {
  //        "page": 1,
  //        "detectedBy": "ruled",
  //        "rows": 8,
  //        "columns": 5,
  //        "cells": [
  //          {
  //            "row": 0,
  //            "col": 0,
  //            "rowSpan": 1,
  //            "colSpan": 1,
  //            "text": "Code"
  //          },
  //          {
  //            "row": 0,
  //            "col": 1,
  //            "rowSpan": 1,
  //            "colSpan": 1,
  //            "text": "Product"
  //          },
  //          ...
  //        ]
  //      }
  //    ],
  //    "quality": {
  //      "rating": "good",
  //      ...
  //    }
  //  }


  pdf.Free;
  json.Free;
  jsonOut.Free;
  sbRow.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.