Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.7.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
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 (three receipts, one per page) is available at three_receipts.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/three_receipts.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")

//  Each of the three pages is a receipt with a "Total:" line.
loo_Json.UpdateString("label","Total")
loo_Json.UpdateString("labelValue","right")

//  By default only the first occurrence (in reading order) is used.
li_Success = loo_Pdf.ExtractText("text",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 "First total: " + loo_JsonOut.StringOf("text")

//  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.
loo_Json.UpdateString("labelOccurrence","all")
li_Success = loo_Pdf.ExtractText("text",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

i = 0
li_Count_i = loo_JsonOut.SizeOfArray("labelMatches")
do while i < li_Count_i
    loo_JsonOut.I = i
    Write-Debug "Page " + string(loo_JsonOut.IntOf("labelMatches[i].page")) + " total: " + loo_JsonOut.StringOf("labelMatches[i].text")
    i = i + 1
loop

//  A number selects one occurrence: 2 is the second.
loo_Json.UpdateInt("labelOccurrence",2)
li_Success = loo_Pdf.ExtractText("text",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 "Second total: " + loo_JsonOut.StringOf("text")

//  The occurrences are counted over the selected pages, so combining "pages" with the label
//  is another way to pick one receipt:
loo_Json.UpdateString("labelOccurrence","first")
loo_Json.UpdateString("pages","3")
loo_Json.UpdateString("label","Receipt No.")
li_Success = loo_Pdf.ExtractText("text",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 "Receipt number on page 3: " + loo_JsonOut.StringOf("text")

//  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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut