Swift Requires Chilkat v11.7.0+
Swift
PDF Placeholders Without a Value: Error, Blank or Keep
See more PDF Text Replacement Examples
Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.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
}
// Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
let json = CkoJsonObject()!
json.updateString(jsonPath: "placeholders.customer", value: "Acme Corp")
json.updateString(jsonPath: "placeholders.date", value: "2026-10-18")
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")
let jsonOut = CkoJsonObject()!
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
success = pdf.textReplace(op: "check", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("error (default): ok = \(jsonOut.bool(of: "ok")), problem: \(jsonOut.string(of: "problems[0].field")!) \(jsonOut.string(of: "problems[0].reason")!)")
// "keep": {{po}} stays on the page, literally.
json.updateString(jsonPath: "unusedPlaceholders", value: "keep")
success = pdf.textReplace(op: "check", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("keep: ok = \(jsonOut.bool(of: "ok"))")
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
json.updateString(jsonPath: "unusedPlaceholders", value: "blank")
success = pdf.textReplace(op: "apply", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("blank: applied = \(jsonOut.bool(of: "applied"))")
success = pdf.writeFile(path: "qa_output/invoice_no_po.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true
}