Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL i
LOCAL lnCount_i

lnSuccess = 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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/three_receipts.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')

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

*  By default only the first occurrence (in reading order) is used.
lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "First total: " + loJsonOut.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.
loJson.UpdateString("labelOccurrence","all")
lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

i = 0
lnCount_i = loJsonOut.SizeOfArray("labelMatches")
DO WHILE i < lnCount_i
    loJsonOut.I = i
    ? "Page " + STR(loJsonOut.IntOf("labelMatches[i].page")) + " total: " + loJsonOut.StringOf("labelMatches[i].text")
    i = i + 1
ENDDO

*  A number selects one occurrence: 2 is the second.
loJson.UpdateInt("labelOccurrence",2)
lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Second total: " + loJsonOut.StringOf("text")

*  The occurrences are counted over the selected pages, so combining "pages" with the label
*  is another way to pick one receipt:
loJson.UpdateString("labelOccurrence","first")
loJson.UpdateString("pages","3")
loJson.UpdateString("label","Receipt No.")
lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Receipt number on page 3: " + loJsonOut.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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut