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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Integer iRowSpan
    Integer iColSpan
    Integer j
    Integer iCount_j
    String sTemp1
    Integer iTemp1
    Integer iTemp2

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/spans_table.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  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.
    Get ComUpdateInt Of hoJson "minRows" 3 To iSuccess
    Get ComUpdateInt Of hoJson "minColumns" 3 To iSuccess

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "tables" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "count" To iTemp1
    Showln "Tables found: " iTemp1
    Get ComIntOf Of hoJsonOut "tables[0].rows" To iTemp1
    Get ComIntOf Of hoJsonOut "tables[0].columns" To iTemp2
    Showln iTemp1 " rows x " iTemp2 " 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.

    Move 0 To j
    Get ComSizeOfArray Of hoJsonOut "tables[0].cells" To iCount_j
    While (j < iCount_j)
        Set ComJ Of hoJsonOut To j
        Get ComIntOf Of hoJsonOut "tables[0].cells[j].rowSpan" To iRowSpan
        Get ComIntOf Of hoJsonOut "tables[0].cells[j].colSpan" To iColSpan
        If ((iRowSpan > 1) Or (iColSpan > 1)) Begin
            Get ComIntOf Of hoJsonOut "tables[0].cells[j].row" To iTemp1
            Get ComIntOf Of hoJsonOut "tables[0].cells[j].col" To iTemp2
            Get ComStringOf Of hoJsonOut "tables[0].cells[j].text" To sTemp1
            Showln "row " iTemp1 ", col " iTemp2 ': "' sTemp1 '" spans ' iRowSpan " row(s) and " iColSpan " column(s)"
        End

        Move (j + 1) To j
    Loop

    //  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)


End_Procedure