Lua Requires Chilkat v11.7.0+
Lua
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.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
-- Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
local json = chilkat.JsonObject.new()
json:update_string("placeholders.customer", "Acme Corp")
json:update_string("placeholders.date", "2026-10-18")
json:update_string("placeholders.qty1", "2")
json:update_string("placeholders.desc1", "Cordless drill, 18 V")
json:update_string("placeholders.price1", "$460.00")
json:update_string("placeholders.amount1", "$920.00")
json:update_string("placeholders.subtotal", "$920.00")
json:update_string("placeholders.taxRate", "7.35%")
json:update_string("placeholders.tax", "$67.62")
json:update_string("placeholders.total", "$987.62")
-- "check" reports what "apply" would do, without changing the PDF.
local jsonOut = chilkat.JsonObject.new()
success = pdf:text_replace("check", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("ok: " .. tostring(jsonOut:bool_of("ok")))
print("would replace: " .. jsonOut:size_of_array("replacements") .. " placeholders")
-- Each problem names the placeholder (field) or label, and a reason:
-- notFound - the placeholder or label is not on the selected pages
-- unusedPlaceholder - a placeholder on the page was given no value
-- missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
-- placeholderSpansFonts - the text is drawn with more than one font or size
local i = 0
local count_i = jsonOut:size_of_array("problems")
while i < count_i do
jsonOut.i = i
print("problem: " .. jsonOut:string_of("problems[i].field") .. jsonOut:string_of("problems[i].label") .. " - " .. jsonOut:string_of("problems[i].reason") .. " (page " .. jsonOut:int_of("problems[i].page") .. ")")
i = i + 1
end
-- Give the missing placeholders their values and check again.
json:update_string("placeholders.po", "PO-88213")
json:update_string("placeholders.qty2", "0")
json:update_string("placeholders.desc2", "(no second item)")
json:update_string("placeholders.price2", "")
json:update_string("placeholders.amount2", "")
success = pdf:text_replace("check", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("ok after adding the values: " .. tostring(jsonOut:bool_of("ok")))
-- Now apply.
if jsonOut:bool_of("ok") then
success = pdf:text_replace("apply", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("applied: " .. tostring(jsonOut:bool_of("applied")))
success = pdf:write_file("qa_output/invoice_checked.pdf")
if not success then
print(pdf.last_error_text)
return
end
end
-- Sample output:
-- ok: false
-- would replace: 10 placeholders
-- problem: po - unusedPlaceholder (page 1)
-- problem: qty2 - unusedPlaceholder (page 1)
-- problem: desc2 - unusedPlaceholder (page 1)
-- problem: price2 - unusedPlaceholder (page 1)
-- problem: amount2 - unusedPlaceholder (page 1)
-- ok after adding the values: true
-- applied: true