Swift Requires Chilkat v11.7.0+
Swift
Fill a PDF Template: Replace {{placeholders}} with Values
See more PDF Text Replacement Examples
Demonstrates the TextReplace method on an invoice template whose values are written as placeholders such as {{customer}} and {{total}}. The "placeholders" object names each placeholder and the text to put in; every occurrence on the page is replaced, including a placeholder inside a sentence or inside a label such as "Tax ({{taxRate}}):". The text is redrawn with the font the template used, so the result looks like the template, and a value that is right-aligned in the template stays right-aligned. The changes are written by WriteFile.Chilkat Swift Downloads
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_template.pdf
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/invoice_template.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// The template has these placeholders in its page text:
// {{customer}} (twice: "Bill To:" and the closing sentence), {{date}}, {{po}},
// two item rows: {{qty1}} {{desc1}} {{price1}} {{amount1}} and {{qty2}} {{desc2}} {{price2}} {{amount2}},
// {{subtotal}}, "Tax ({{taxRate}}):" with {{tax}}, and {{total}}.
let json = CkoJsonObject()!
json.updateString(jsonPath: "placeholders.customer", value: "Acme Corp")
json.updateString(jsonPath: "placeholders.date", value: "2026-10-18")
json.updateString(jsonPath: "placeholders.po", value: "PO-88213")
json.updateString(jsonPath: "placeholders.qty1", value: "2")
json.updateString(jsonPath: "placeholders.desc1", value: "Cordless drill, 18 V")
json.updateString(jsonPath: "placeholders.price1", value: "$460.00")
json.updateString(jsonPath: "placeholders.amount1", value: "$920.00")
json.updateString(jsonPath: "placeholders.qty2", value: "6")
json.updateString(jsonPath: "placeholders.desc2", value: "Galvanized hinge, 4 inch")
json.updateString(jsonPath: "placeholders.price2", value: "$12.50")
json.updateString(jsonPath: "placeholders.amount2", value: "$75.00")
json.updateString(jsonPath: "placeholders.subtotal", value: "$995.00")
json.updateString(jsonPath: "placeholders.taxRate", value: "7.35%")
json.updateString(jsonPath: "placeholders.tax", value: "$73.13")
json.updateString(jsonPath: "placeholders.total", value: "$1,068.13")
// "apply" makes every replacement, or none: if any value cannot be drawn with the
// template's font, the method returns false, the PDF is unchanged, and jsonOut says why.
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
}
}
// One entry per placeholder: the pages it was found on and how many times it was replaced.
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")!): \(jsonOut.int(of: "replacements[i].count").intValue) on page(s) \(jsonOut.string(of: "replacements[i].pages")!)")
i = i + 1
}
// The replacements stay in memory until the PDF is written.
success = pdf.writeFile(path: "qa_output/invoice_filled.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("Wrote qa_output/invoice_filled.pdf")
// Sample output:
// customer: 2 on page(s) 1
// date: 1 on page(s) 1
// po: 1 on page(s) 1
// qty1: 1 on page(s) 1
// desc1: 1 on page(s) 1
// price1: 1 on page(s) 1
// amount1: 1 on page(s) 1
// qty2: 1 on page(s) 1
// desc2: 1 on page(s) 1
// price2: 1 on page(s) 1
// amount2: 1 on page(s) 1
// subtotal: 1 on page(s) 1
// taxRate: 1 on page(s) 1
// tax: 1 on page(s) 1
// total: 1 on page(s) 1
// Wrote qa_output/invoice_filled.pdf
}