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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
long li_RowSpan
long li_ColSpan
long j
long li_Count_j

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/spans_table.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("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.
loo_Json.UpdateInt("minRows",3)
loo_Json.UpdateInt("minColumns",3)

li_Success = loo_Pdf.ExtractText("tables",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Tables found: " + string(loo_JsonOut.IntOf("count"))
Write-Debug string(loo_JsonOut.IntOf("tables[0].rows")) + " rows x " + string(loo_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.

j = 0
li_Count_j = loo_JsonOut.SizeOfArray("tables[0].cells")
do while j < li_Count_j
    loo_JsonOut.J = j
    li_RowSpan = loo_JsonOut.IntOf("tables[0].cells[j].rowSpan")
    li_ColSpan = loo_JsonOut.IntOf("tables[0].cells[j].colSpan")
    if (li_RowSpan > 1) OR (li_ColSpan > 1) then
        Write-Debug "row " + string(loo_JsonOut.IntOf("tables[0].cells[j].row")) + ", col " + string(loo_JsonOut.IntOf("tables[0].cells[j].col")) + ": ~"" + loo_JsonOut.StringOf("tables[0].cells[j].text") + "~" spans " + string(li_RowSpan) + " row(s) and " + string(li_ColSpan) + " column(s)"
    end if

    j = j + 1
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)


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut