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

Replace the Value After a Label in a PDF (Re-issue an Invoice)

See more PDF Text Replacement Examples

Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.

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

    //  The invoice has lines such as "Invoice Number:   INV-2026-0412" and "Total Due:  $1,234.53".
    //  Each label's new value replaces whatever follows the label on its line.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "labels[0].label", value: "Invoice Number:")
    json.updateString(jsonPath: "labels[0].value", value: "INV-2026-0500")
    json.updateString(jsonPath: "labels[1].label", value: "Invoice Date:")
    json.updateString(jsonPath: "labels[1].value", value: "April 2, 2026")
    json.updateString(jsonPath: "labels[2].label", value: "customer")
    json.updateString(jsonPath: "labels[2].value", value: "Contoso Ltd")
    json.updateString(jsonPath: "labels[3].label", value: "Total Due:")
    json.updateString(jsonPath: "labels[3].value", value: "$1,999.99")

    let jsonOut = CkoJsonObject()!
    success = pdf.textReplace(op: "apply", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("Not applied: \(jsonOut.emit()!)")
        if success == false {
            print("\(pdf.lastErrorText!)")
            return
        }

    }

    var i: Int = 0
    var count_i: Int = jsonOut.size(ofArray: "replacements").intValue
    while i < count_i {
        jsonOut.i = i
        print("Replaced the value of \"\(jsonOut.string(of: "replacements[i].label")!)\" on page \(jsonOut.string(of: "replacements[i].pages")!)")
        i = i + 1
    }

    success = pdf.writeFile(path: "qa_output/invoice_reissued.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  Read the new values back with ExtractText's label option.
    let jsonRead = CkoJsonObject()!
    let jsonText = CkoJsonObject()!
    jsonRead.updateString(jsonPath: "label", value: "Total Due")
    jsonRead.updateString(jsonPath: "labelValue", value: "right")
    success = pdf.extractText(op: "text", jsonIn: jsonRead, jsonOut: jsonText)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("Total Due is now: \(jsonText.string(of: "text")!)")

    //  Sample output:

    //  Replaced the value of "Invoice Number:" on page 1
    //  Replaced the value of "Invoice Date:" on page 1
    //  Replaced the value of "customer" on page 1
    //  Replaced the value of "Total Due:" on page 1
    //  Total Due is now: $1,999.99

}