AutoIt Requires Chilkat v11.7.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = 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
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/spans_table.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("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.
$oJson.UpdateInt("minRows",3)
$oJson.UpdateInt("minColumns",3)
$bSuccess = $oPdf.ExtractText("tables",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Tables found: " & $oJsonOut.IntOf("count") & @CRLF)
ConsoleWrite($oJsonOut.IntOf("tables[0].rows") & " rows x " & $oJsonOut.IntOf("tables[0].columns") & " columns" & @CRLF)
; A merged cell is reported once, at its top-left position, with its spans.
; The positions it covers have no cell of their own.
Local $iRowSpan
Local $iColSpan
Local $iJ = 0
Local $iCount_j = $oJsonOut.SizeOfArray("tables[0].cells")
While $iJ < $iCount_j
$oJsonOut.J = $iJ
$iRowSpan = $oJsonOut.IntOf("tables[0].cells[j].rowSpan")
$iColSpan = $oJsonOut.IntOf("tables[0].cells[j].colSpan")
If (($iRowSpan > 1) Or ($iColSpan > 1)) Then
ConsoleWrite("row " & $oJsonOut.IntOf("tables[0].cells[j].row") & ", col " & $oJsonOut.IntOf("tables[0].cells[j].col") & ": """ & $oJsonOut.StringOf("tables[0].cells[j].text") _
& """ spans " & $iRowSpan & " row(s) and " & $iColSpan & " column(s)" & @CRLF)
EndIf
$iJ = $iJ + 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)