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

Get a PDF Field Value by its Label (Value to the Right)

See more PDF Text Extraction Examples

Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.

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 invoice used in this example is available at invoice_labels.pdf

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

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

    //  The invoice has lines such as "Invoice Number:   INV-2026-0412".
    //  The label option restricts the "text" operation to the text belonging to the label.
    json.updateString(jsonPath: "label", value: "Invoice Number")

    //  "right" means the value is the rest of the label's line.
    //  (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
    json.updateString(jsonPath: "labelValue", value: "right")

    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  The value is returned as the text.
    print("Invoice Number: \(jsonOut.string(of: "text")!)")

    //  The labelMatches array tells where the label was found and how it appeared in the PDF.
    print("Found on page \(jsonOut.int(of: "labelMatches[0].page").intValue) as \"\(jsonOut.string(of: "labelMatches[0].label")!)\"")

    //  Matching ignores case and the trailing colon, so these all work:
    json.updateString(jsonPath: "label", value: "CUSTOMER:")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

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

    json.updateString(jsonPath: "label", value: "due date")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("Due Date: \(jsonOut.string(of: "text")!)")

    //  The totals at the foot of the invoice are right-aligned labels with the amount beside them.
    json.updateString(jsonPath: "label", value: "Total Due")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("Total Due: \(jsonOut.string(of: "text")!)")

    //  A label that is not found is not an error.  The text is empty, labelMatches is empty,
    //  and the quality report has an issue with the code "labelNotFound".
    json.updateString(jsonPath: "label", value: "Tracking Number")
    success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    if jsonOut.size(ofArray: "labelMatches").intValue == 0 {
        print("Tracking Number: (not found) \(jsonOut.string(of: "quality.pages[0].issues[0].code")!)")
    }

    //  Sample output:

    //  Invoice Number: INV-2026-0412
    //  Found on page 1 as "Invoice Number:"
    //  Customer: Northwind Traders
    //  Due Date: April 13, 2026
    //  Total Due: $1,234.53
    //  Tracking Number: (not found) labelNotFound

}