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

Swift

func chilkatTest() {
    var success: Bool = 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

    let pdf = CkoPdf()!
    success = pdf.loadFile(filePath: "qa_data/pdf/three_receipts.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    let json = CkoJsonObject()!
    let jsonOut = CkoJsonObject()!

    //  Each of the three pages is a receipt with a "Total:" line.
    json.updateString(jsonPath: "label", value: "Total")
    json.updateString(jsonPath: "labelValue", value: "right")

    //  By default only the first occurrence (in reading order) is used.
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("First total: \(jsonOut.string(of: "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.
    json.updateString(jsonPath: "labelOccurrence", value: "all")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    var i: Int = 0
    var count_i: Int = jsonOut.size(ofArray: "labelMatches").intValue
    while i < count_i {
        jsonOut.i = i
        print("Page \(jsonOut.int(of: "labelMatches[i].page").intValue) total: \(jsonOut.string(of: "labelMatches[i].text")!)")
        i = i + 1
    }

    //  A number selects one occurrence: 2 is the second.
    json.updateInt(jsonPath: "labelOccurrence", value: 2)
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("Second total: \(jsonOut.string(of: "text")!)")

    //  The occurrences are counted over the selected pages, so combining "pages" with the label
    //  is another way to pick one receipt:
    json.updateString(jsonPath: "labelOccurrence", value: "first")
    json.updateString(jsonPath: "pages", value: "3")
    json.updateString(jsonPath: "label", value: "Receipt No.")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("Receipt number on page 3: \(jsonOut.string(of: "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

}