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

Tcl

load ./chilkat.dll

set success 0

#  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

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/price_list.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  The "tables" operation returns the tables found on the selected pages.
#  Tables are recognized by their drawn cell borders.
set success [CkPdf_ExtractText $pdf "tables" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "Number of tables: [CkJsonObject_IntOf $jsonOut count]"

#  Used to build each row of output.
set sbRow [new_CkStringBuilder]

set i 0
set count_i [CkJsonObject_SizeOfArray $jsonOut "tables"]
while {$i < $count_i} {
    CkJsonObject_put_I $jsonOut $i
    puts "Table on page [CkJsonObject_IntOf $jsonOut {tables[i].page}]: [CkJsonObject_IntOf $jsonOut {tables[i].rows}] rows x [CkJsonObject_IntOf $jsonOut {tables[i].columns}] columns, detected by [CkJsonObject_stringOf $jsonOut {tables[i].detectedBy}]"

    #  The cells are listed row by row.  Print each row as its cells separated by " | ".
    set currentRow -1
    CkStringBuilder_SetString $sbRow ""
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jsonOut "tables[i].cells"]
    while {$j < $count_j} {
        CkJsonObject_put_J $jsonOut $j
        set row [CkJsonObject_IntOf $jsonOut "tables[i].cells[j].row"]
        if {$row != $currentRow} then {
            if {$currentRow >= 0} then {
                puts [CkStringBuilder_getAsString $sbRow]
            }

            set currentRow $row
            CkStringBuilder_SetString $sbRow "  "
        }         else {
            CkStringBuilder_Append $sbRow " | "
        }

        CkStringBuilder_Append $sbRow [CkJsonObject_stringOf $jsonOut "tables[i].cells[j].text"]
        set j [expr $j + 1]
    }
    puts [CkStringBuilder_getAsString $sbRow]
    set i [expr $i + 1]
}

#  The cells of the first table as JSON:
CkJsonObject_put_EmitCompact $jsonOut 0
puts [CkJsonObject_emit $jsonOut]

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

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkStringBuilder $sbRow