DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoSbRow
Integer iCurrentRow
Integer iRow
Integer j
Integer iCount_j
Integer i
Integer iCount_i
String sTemp1
Integer iTemp1
Integer iTemp2
Integer iTemp3
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 price_list.pdf
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/price_list.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
// The "tables" operation returns the tables found on the selected pages.
// Tables are recognized by their drawn cell borders.
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 "Number of tables: " iTemp1
// Used to build each row of output.
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRow
If (Not(IsComObjectCreated(hoSbRow))) Begin
Send CreateComObject of hoSbRow
End
Move 0 To i
Get ComSizeOfArray Of hoJsonOut "tables" To iCount_i
While (i < iCount_i)
Set ComI Of hoJsonOut To i
Get ComIntOf Of hoJsonOut "tables[i].page" To iTemp1
Get ComIntOf Of hoJsonOut "tables[i].rows" To iTemp2
Get ComIntOf Of hoJsonOut "tables[i].columns" To iTemp3
Get ComStringOf Of hoJsonOut "tables[i].detectedBy" To sTemp1
Showln "Table on page " iTemp1 ": " iTemp2 " rows x " iTemp3 " columns, detected by " sTemp1
// The cells are listed row by row. Print each row as its cells separated by " | ".
Move -1 To iCurrentRow
Get ComSetString Of hoSbRow "" To iSuccess
Move 0 To j
Get ComSizeOfArray Of hoJsonOut "tables[i].cells" To iCount_j
While (j < iCount_j)
Set ComJ Of hoJsonOut To j
Get ComIntOf Of hoJsonOut "tables[i].cells[j].row" To iRow
If (iRow <> iCurrentRow) Begin
If (iCurrentRow >= 0) Begin
Get ComGetAsString Of hoSbRow To sTemp1
Showln sTemp1
End
Move iRow To iCurrentRow
Get ComSetString Of hoSbRow " " To iSuccess
End
Else Begin
Get ComAppend Of hoSbRow " | " To iSuccess
End
Get ComStringOf Of hoJsonOut "tables[i].cells[j].text" To sTemp1
Get ComAppend Of hoSbRow sTemp1 To iSuccess
Move (j + 1) To j
Loop
Get ComGetAsString Of hoSbRow To sTemp1
Showln sTemp1
Move (i + 1) To i
Loop
// The cells of the first table as JSON:
Set ComEmitCompact Of hoJsonOut To False
Get ComEmit Of hoJsonOut To sTemp1
Showln sTemp1
// 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",
// ...
// }
// }
End_Procedure