PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Extract Tables from a PDF
See more PDF Text Extraction Examples
Demonstrates the "tables" operation, which returns the tables detected on the pages as rows and cells. Each cell has its row and column (counted from 0), its row and column span, and its text.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_SbRow
long li_CurrentRow
long li_Row
long j
long li_Count_j
long i
long li_Count_i
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 price_list.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/price_list.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")
// The "tables" operation returns the tables found on the selected pages.
// Tables are recognized by their drawn cell borders.
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 "Number of tables: " + string(loo_JsonOut.IntOf("count"))
// Used to build each row of output.
loo_SbRow = create oleobject
li_rc = loo_SbRow.ConnectToNewObject("Chilkat.StringBuilder")
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("tables")
do while i < li_Count_i
loo_JsonOut.I = i
Write-Debug "Table on page " + string(loo_JsonOut.IntOf("tables[i].page")) + ": " + string(loo_JsonOut.IntOf("tables[i].rows")) + " rows x " + string(loo_JsonOut.IntOf("tables[i].columns")) + " columns, detected by " + loo_JsonOut.StringOf("tables[i].detectedBy")
// The cells are listed row by row. Print each row as its cells separated by " | ".
li_CurrentRow = -1
loo_SbRow.SetString("")
j = 0
li_Count_j = loo_JsonOut.SizeOfArray("tables[i].cells")
do while j < li_Count_j
loo_JsonOut.J = j
li_Row = loo_JsonOut.IntOf("tables[i].cells[j].row")
if li_Row <> li_CurrentRow then
if li_CurrentRow >= 0 then
Write-Debug loo_SbRow.GetAsString()
end if
li_CurrentRow = li_Row
loo_SbRow.SetString(" ")
else
loo_SbRow.Append(" | ")
end if
loo_SbRow.Append(loo_JsonOut.StringOf("tables[i].cells[j].text"))
j = j + 1
loop
Write-Debug loo_SbRow.GetAsString()
i = i + 1
loop
// The cells of the first table as JSON:
loo_JsonOut.EmitCompact = 0
Write-Debug loo_JsonOut.Emit()
// Sample output:
// Number of tables: 1
// Table on page 1: 8 rows x 5 columns, detected by ruled
// Code | Product | Unit | Price | In Stock
// HT-101 | Claw hammer, 16 oz | each | $18.00 | 24
// HT-102 | Adjustable wrench, 10 in | each | $14.50 | 31
// HT-103 | Screwdriver set, 6 piece | set | $22.00 | 12
// HT-104 | Tape measure, 25 ft | each | $9.75 | 40
// HT-105 | Utility knife | each | $6.20 | 58
// HT-106 | Pliers, needle nose | each | $11.00 | 19
// HT-107 | Hand saw, 20 in | each | $21.40 | 8
// {
// "count": 1,
// "tables": [
// {
// "page": 1,
// "detectedBy": "ruled",
// "rows": 8,
// "columns": 5,
// "cells": [
// {
// "row": 0,
// "col": 0,
// "rowSpan": 1,
// "colSpan": 1,
// "text": "Code"
// },
// {
// "row": 0,
// "col": 1,
// "rowSpan": 1,
// "colSpan": 1,
// "text": "Product"
// },
// ...
// ]
// }
// ],
// "quality": {
// "rating": "good",
// ...
// }
// }
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SbRow