DataFlex Requires Chilkat v11.7.0+
DataFlex
Get Every Occurrence of a Label in a PDF
See more PDF Text Extraction Examples
Demonstrates the "labelOccurrence" option. When the same label appears more than once (for example "Total:" on each of several receipts), "first" (the default) uses the first occurrence, "all" returns every occurrence with its page, and a number selects one occurrence.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Integer i
Integer iCount_i
String sTemp1
Integer iTemp1
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 (three receipts, one per page) is available at three_receipts.pdf
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/three_receipts.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
// Each of the three pages is a receipt with a "Total:" line.
Get ComUpdateString Of hoJson "label" "Total" To iSuccess
Get ComUpdateString Of hoJson "labelValue" "right" To iSuccess
// By default only the first occurrence (in reading order) is used.
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "First total: " sTemp1
// labelOccurrence "all" returns every occurrence. The "text" member holds the values joined
// by line breaks, and labelMatches has one entry per occurrence with its page.
Get ComUpdateString Of hoJson "labelOccurrence" "all" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Move 0 To i
Get ComSizeOfArray Of hoJsonOut "labelMatches" To iCount_i
While (i < iCount_i)
Set ComI Of hoJsonOut To i
Get ComIntOf Of hoJsonOut "labelMatches[i].page" To iTemp1
Get ComStringOf Of hoJsonOut "labelMatches[i].text" To sTemp1
Showln "Page " iTemp1 " total: " sTemp1
Move (i + 1) To i
Loop
// A number selects one occurrence: 2 is the second.
Get ComUpdateInt Of hoJson "labelOccurrence" 2 To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Second total: " sTemp1
// The occurrences are counted over the selected pages, so combining "pages" with the label
// is another way to pick one receipt:
Get ComUpdateString Of hoJson "labelOccurrence" "first" To iSuccess
Get ComUpdateString Of hoJson "pages" "3" To iSuccess
Get ComUpdateString Of hoJson "label" "Receipt No." To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Receipt number on page 3: " sTemp1
// Sample output:
// First total: $19.86
// Page 1 total: $19.86
// Page 2 total: $17.34
// Page 3 total: $23.62
// Second total: $17.34
// Receipt number on page 3: 1003
End_Procedure