Lua Requires Chilkat v11.7.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/invoice_template.pdf")
if not success then
print(pdf.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json:update_string("placeholders.customer", "Acme Corp")
-- (The other placeholders are left as they are for this example.)
json:update_string("unusedPlaceholders", "keep")
local jsonOut = chilkat.JsonObject.new()
success = pdf:text_replace("apply", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("In the page content, customer was replaced " .. jsonOut:int_of("replacements[0].count") .. " times.")
-- Where else the placeholder occurs, untouched:
local i = 0
local count_i = jsonOut:size_of_array("notReplaced")
while i < count_i do
jsonOut.i = i
print("not replaced: {{" .. jsonOut:string_of("notReplaced[i].field") .. "}} in " .. jsonOut:string_of("notReplaced[i].where"))
i = i + 1
end
success = pdf:write_file("qa_output/invoice_customer.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- Sample output:
-- In the page content, customer was replaced 2 times.
-- not replaced: {{customer}} in metadata
-- not replaced: {{customer}} in linkTarget