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

Extract a PDF Table with Merged Cells

See more PDF Text Extraction Examples

Demonstrates how merged cells are reported by the "tables" operation: a header cell that spans several columns has colSpan greater than 1, and a row label that spans several rows has rowSpan greater than 1. Also shows the minRows and minColumns options.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.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 spans_table.pdf

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

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/spans_table.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

    ;  Grids smaller than minRows x minColumns are not reported as tables.  The defaults are 2 and 2.
    ;  Setting them higher skips small boxes that are not really tables.
    CkJsonObject::ckUpdateInt(json,"minRows",3)
    CkJsonObject::ckUpdateInt(json,"minColumns",3)

    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 "Tables found: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
    Debug Str(CkJsonObject::ckIntOf(jsonOut,"tables[0].rows")) + " rows x " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[0].columns")) + " columns"

    ;  A merged cell is reported once, at its top-left position, with its spans.
    ;  The positions it covers have no cell of their own.
    rowSpan.i
    colSpan.i
    j.i = 0
    count_j.i = CkJsonObject::ckSizeOfArray(jsonOut,"tables[0].cells")
    While j < count_j
        CkJsonObject::setCkJ(jsonOut, j)
        rowSpan = CkJsonObject::ckIntOf(jsonOut,"tables[0].cells[j].rowSpan")
        colSpan = CkJsonObject::ckIntOf(jsonOut,"tables[0].cells[j].colSpan")
        If (rowSpan > 1) OR (colSpan > 1)
            Debug "row " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[0].cells[j].row")) + ", col " + Str(CkJsonObject::ckIntOf(jsonOut,"tables[0].cells[j].col")) + ": " + Chr(34) + CkJsonObject::ckStringOf(jsonOut,"tables[0].cells[j].text") + Chr(34) + " spans " + Str(rowSpan) + " row(s) and " + Str(colSpan) + " column(s)"
        EndIf

        j = j + 1
    Wend

    ;  Sample output:

    ;  Tables found: 1
    ;  6 rows x 6 columns
    ;  row 0, col 0: "Area" spans 2 row(s) and 1 column(s)
    ;  row 0, col 1: "City" spans 2 row(s) and 1 column(s)
    ;  row 0, col 2: "First Half" spans 1 row(s) and 2 column(s)
    ;  row 0, col 4: "Second Half" spans 1 row(s) and 2 column(s)
    ;  row 2, col 0: "East" spans 2 row(s) and 1 column(s)
    ;  row 4, col 0: "West" spans 2 row(s) and 1 column(s)


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


    ProcedureReturn
EndProcedure