Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.7.0+

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 Lua Downloads

Lua
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 every placeholder except {{po}} (the purchase order number, which this customer has none of).
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.qty2", "6")
json:update_string("placeholders.desc2", "Galvanized hinge, 4 inch")
json:update_string("placeholders.price2", "$12.50")
json:update_string("placeholders.amount2", "$75.00")
json:update_string("placeholders.subtotal", "$995.00")
json:update_string("placeholders.taxRate", "7.35%")
json:update_string("placeholders.tax", "$73.13")
json:update_string("placeholders.total", "$1,068.13")

local jsonOut = chilkat.JsonObject.new()

-- The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
success = pdf:text_replace("check", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("error (default): ok = " .. tostring(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:update_string("unusedPlaceholders", "keep")
success = pdf:text_replace("check", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("keep: ok = " .. tostring(jsonOut:bool_of("ok")))

-- "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
json:update_string("unusedPlaceholders", "blank")
success = pdf:text_replace("apply", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("blank: applied = " .. tostring(jsonOut:bool_of("applied")))

success = pdf:write_file("qa_output/invoice_no_po.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

-- Sample output:

-- error (default): ok = false, problem: po unusedPlaceholder
-- keep: ok = true
-- blank: applied = true