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

PDF Placeholders with Other Delimiters: [[name]]

See more PDF Text Replacement Examples

Demonstrates the "placeholderSyntax" option. Placeholders are {{name}} by default; a template written with other delimiters, such as [[name]] or <>, says so with placeholderSyntax, written around the word "name".

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 letter_template.pdf

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

    //  The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "placeholderSyntax", value: "[[name]]")
    json.updateString(jsonPath: "placeholders.date", value: "October 18, 2026")
    json.updateString(jsonPath: "placeholders.name", value: "Ms. Rivera")
    json.updateString(jsonPath: "placeholders.expiry", value: "December 31, 2026")
    json.updateString(jsonPath: "placeholders.amount", value: "$299.00")
    json.updateString(jsonPath: "placeholders.agent", value: "Dana Whitfield, Account Manager")

    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("[[\(jsonOut.string(of: "replacements[i].field")!)]] replaced")
        i = i + 1
    }

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

    //  Sample output:

    //  [[date]] replaced
    //  [[name]] replaced
    //  [[expiry]] replaced
    //  [[amount]] replaced
    //  [[agent]] replaced

}