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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPdf.pb"

Procedure ChilkatExample()

    success.i = 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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/price_list.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  The "tables" operation returns the tables found on the selected pages.
    ;  Tables are recognized by their drawn cell borders.
    success = CkPdf::ckExtractText(pdf,"tables",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "Number of tables: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))

    ;  Used to build each row of output.
    sbRow.i = CkStringBuilder::ckCreate()
    If sbRow.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    currentRow.i
    row.i
    j.i
    count_j.i

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"tables")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "Table on page " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[i].page")) + ": " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[i].rows")) + " rows x " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[i].columns")) + " columns, detected by " + CkJsonObject::ckStringOf(jsonOut,"tables[i].detectedBy")

        ;  The cells are listed row by row.  Print each row as its cells separated by " | ".
        currentRow = -1
        CkStringBuilder::ckSetString(sbRow,"")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jsonOut,"tables[i].cells")
        While j < count_j
            CkJsonObject::setCkJ(jsonOut, j)
            row = CkJsonObject::ckIntOf(jsonOut,"tables[i].cells[j].row")
            If row <> currentRow
                If currentRow >= 0
                    Debug CkStringBuilder::ckGetAsString(sbRow)
                EndIf

                currentRow = row
                CkStringBuilder::ckSetString(sbRow,"  ")
            Else
                CkStringBuilder::ckAppend(sbRow," | ")
            EndIf

            CkStringBuilder::ckAppend(sbRow,CkJsonObject::ckStringOf(jsonOut,"tables[i].cells[j].text"))
            j = j + 1
        Wend
        Debug CkStringBuilder::ckGetAsString(sbRow)
        i = i + 1
    Wend

    ;  The cells of the first table as JSON:
    CkJsonObject::setCkEmitCompact(jsonOut, 0)
    Debug CkJsonObject::ckEmit(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",
    ;      ...
    ;    }
    ;  }


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkStringBuilder::ckDispose(sbRow)


    ProcedureReturn
EndProcedure