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

Xojo Plugin
Dim success As Boolean
success = False

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

Dim pdf As New Chilkat.Pdf
success = pdf.LoadFile("qa_data/pdf/spans_table.pdf")
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

Dim json As New Chilkat.JsonObject
Dim jsonOut As New Chilkat.JsonObject

//  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.
success = json.UpdateInt("minRows",3)
success = json.UpdateInt("minColumns",3)

success = pdf.ExtractText("tables",json,jsonOut)
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

System.DebugLog("Tables found: " + Str(jsonOut.IntOf("count")))
System.DebugLog(Str(jsonOut.IntOf("tables[0].rows")) + " rows x " + Str(jsonOut.IntOf("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.
Dim rowSpan As Int32
Dim colSpan As Int32
Dim j As Int32
j = 0
Dim count_j As Int32
count_j = jsonOut.SizeOfArray("tables[0].cells")
While j < count_j
    jsonOut.J = j
    rowSpan = jsonOut.IntOf("tables[0].cells[j].rowSpan")
    colSpan = jsonOut.IntOf("tables[0].cells[j].colSpan")
    If ((rowSpan > 1) Or (colSpan > 1)) Then
        System.DebugLog("row " + Str(jsonOut.IntOf("tables[0].cells[j].row")) + ", col " + Str(jsonOut.IntOf("tables[0].cells[j].col")) + ": """ + jsonOut.StringOf("tables[0].cells[j].text") _
             + """ spans " + Str(rowSpan) + " row(s) and " + Str(colSpan) + " column(s)")
    End If

    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)