Swift Requires Chilkat v11.7.0+
Swift
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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
}
let json = CkoJsonObject()!
json.updateString(jsonPath: "placeholders.customer", value: "Acme Corp")
// (The other placeholders are left as they are for this example.)
json.updateString(jsonPath: "unusedPlaceholders", value: "keep")
let jsonOut = CkoJsonObject()!
success = pdf.textReplace(op: "apply", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("In the page content, customer was replaced \(jsonOut.int(of: "replacements[0].count").intValue) times.")
// Where else the placeholder occurs, untouched:
var i: Int = 0
var count_i: Int = jsonOut.size(ofArray: "notReplaced").intValue
while i < count_i {
jsonOut.i = i
print("not replaced: {{\(jsonOut.string(of: "notReplaced[i].field")!)}} in \(jsonOut.string(of: "notReplaced[i].where")!)")
i = i + 1
}
success = pdf.writeFile(path: "qa_output/invoice_customer.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// Sample output:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget
}