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

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oSbRow
LOCAL nCurrentRow
LOCAL nRow
LOCAL j
LOCAL nCount_j
LOCAL i
LOCAL nCount_i

nSuccess := 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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/price_list.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

//  The "tables" operation returns the tables found on the selected pages.
//  Tables are recognized by their drawn cell borders.
nSuccess := oPdf:ExtractText("tables", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "Number of tables: " + Str(oJsonOut:IntOf("count"))

//  Used to build each row of output.
oSbRow := CreateObject("Chilkat.StringBuilder")

i := 0
nCount_i := oJsonOut:SizeOfArray("tables")
DO WHILE i < nCount_i
    oJsonOut:I := i
    ? "Table on page " + Str(oJsonOut:IntOf("tables[i].page")) + ": " + Str(oJsonOut:IntOf("tables[i].rows")) + " rows x " + Str(oJsonOut:IntOf("tables[i].columns")) + " columns, detected by " + oJsonOut:StringOf("tables[i].detectedBy")

    //  The cells are listed row by row.  Print each row as its cells separated by " | ".
    nCurrentRow := -1
    oSbRow:SetString("")
    j := 0
    nCount_j := oJsonOut:SizeOfArray("tables[i].cells")
    DO WHILE j < nCount_j
        oJsonOut:J := j
        nRow := oJsonOut:IntOf("tables[i].cells[j].row")
        IF (nRow != nCurrentRow)
            IF (nCurrentRow >= 0)
                ? oSbRow:GetAsString()
            ENDIF

            nCurrentRow := nRow
            oSbRow:SetString("  ")
        ELSE
            oSbRow:Append(" | ")
        ENDIF

        oSbRow:Append(oJsonOut:StringOf("tables[i].cells[j].text"))
        j := j + 1
    ENDDO
    ? oSbRow:GetAsString()
    i := i + 1
ENDDO

//  The cells of the first table as JSON:
oJsonOut:EmitCompact := 0
? oJsonOut: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",
//      ...
//    }
//  }

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oSbRow:destroy()