AutoIt Requires Chilkat v11.7.0+
AutoIt
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 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 (three receipts, one per page) is available at three_receipts.pdf
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/three_receipts.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
; Each of the three pages is a receipt with a "Total:" line.
$oJson.UpdateString("label","Total")
$oJson.UpdateString("labelValue","right")
; By default only the first occurrence (in reading order) is used.
$bSuccess = $oPdf.ExtractText("text",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("First total: " & $oJsonOut.StringOf("text") & @CRLF)
; 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.
$oJson.UpdateString("labelOccurrence","all")
$bSuccess = $oPdf.ExtractText("text",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
Local $i = 0
Local $iCount_i = $oJsonOut.SizeOfArray("labelMatches")
While $i < $iCount_i
$oJsonOut.I = $i
ConsoleWrite("Page " & $oJsonOut.IntOf("labelMatches[i].page") & " total: " & $oJsonOut.StringOf("labelMatches[i].text") & @CRLF)
$i = $i + 1
Wend
; A number selects one occurrence: 2 is the second.
$oJson.UpdateInt("labelOccurrence",2)
$bSuccess = $oPdf.ExtractText("text",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Second total: " & $oJsonOut.StringOf("text") & @CRLF)
; The occurrences are counted over the selected pages, so combining "pages" with the label
; is another way to pick one receipt:
$oJson.UpdateString("labelOccurrence","first")
$oJson.UpdateString("pages","3")
$oJson.UpdateString("label","Receipt No.")
$bSuccess = $oPdf.ExtractText("text",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Receipt number on page 3: " & $oJsonOut.StringOf("text") & @CRLF)
; 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