Swift Requires Chilkat v11.7.0+
Swift
A PDF Label That Is Not on the Page: notFound or Ignore
See more PDF Text Replacement Examples
Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.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_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()!
// "Tracking Number:" is not on this invoice.
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: "Tracking Number:")
json.updateString(jsonPath: "labels[1].value", value: "1Z999AA10123456784")
success = pdf.textReplace(op: "check", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("default: ok = \(jsonOut.bool(of: "ok")), problem: \"\(jsonOut.string(of: "problems[0].label")!)\" \(jsonOut.string(of: "problems[0].reason")!)")
// With "ignore", the label that is not there is skipped.
json.updateString(jsonPath: "unmatchedValues", value: "ignore")
success = pdf.textReplace(op: "apply", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("ignore: applied = \(jsonOut.bool(of: "applied")), replacements: \(jsonOut.size(ofArray: "replacements").intValue)")
success = pdf.writeFile(path: "qa_output/invoice_renumbered.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// Sample output:
// default: ok = false, problem: "Tracking Number:" notFound
// ignore: applied = true, replacements: 1
}