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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/three_receipts.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Each of the three pages is a receipt with a "Total:" line.
    CkJsonObject::ckUpdateString(json,"label","Total")
    CkJsonObject::ckUpdateString(json,"labelValue","right")

    ;  By default only the first occurrence (in reading order) is used.
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "First total: " + CkJsonObject::ckStringOf(jsonOut,"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.
    CkJsonObject::ckUpdateString(json,"labelOccurrence","all")
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"labelMatches")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "Page " + Str(CkJsonObject::ckIntOf(jsonOut,"labelMatches[i].page")) + " total: " + CkJsonObject::ckStringOf(jsonOut,"labelMatches[i].text")
        i = i + 1
    Wend

    ;  A number selects one occurrence: 2 is the second.
    CkJsonObject::ckUpdateInt(json,"labelOccurrence",2)
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "Second total: " + CkJsonObject::ckStringOf(jsonOut,"text")

    ;  The occurrences are counted over the selected pages, so combining "pages" with the label
    ;  is another way to pick one receipt:
    CkJsonObject::ckUpdateString(json,"labelOccurrence","first")
    CkJsonObject::ckUpdateString(json,"pages","3")
    CkJsonObject::ckUpdateString(json,"label","Receipt No.")
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "Receipt number on page 3: " + CkJsonObject::ckStringOf(jsonOut,"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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure