Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
nSuccess := 0
// 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
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/invoice_template.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
// Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("placeholders.customer", "Acme Corp")
oJson:UpdateString("placeholders.date", "2026-10-18")
oJson:UpdateString("placeholders.qty1", "2")
oJson:UpdateString("placeholders.desc1", "Cordless drill, 18 V")
oJson:UpdateString("placeholders.price1", "$460.00")
oJson:UpdateString("placeholders.amount1", "$920.00")
oJson:UpdateString("placeholders.qty2", "6")
oJson:UpdateString("placeholders.desc2", "Galvanized hinge, 4 inch")
oJson:UpdateString("placeholders.price2", "$12.50")
oJson:UpdateString("placeholders.amount2", "$75.00")
oJson:UpdateString("placeholders.subtotal", "$995.00")
oJson:UpdateString("placeholders.taxRate", "7.35%")
oJson:UpdateString("placeholders.tax", "$73.13")
oJson:UpdateString("placeholders.total", "$1,068.13")
oJsonOut := CreateObject("Chilkat.JsonObject")
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
nSuccess := oPdf:TextReplace("check", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "error (default): ok = " + Str(oJsonOut:BoolOf("ok")) + ", problem: " + oJsonOut:StringOf("problems[0].field") + " " + oJsonOut:StringOf("problems[0].reason")
// "keep": {{po}} stays on the page, literally.
oJson:UpdateString("unusedPlaceholders", "keep")
nSuccess := oPdf:TextReplace("check", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "keep: ok = " + Str(oJsonOut:BoolOf("ok"))
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
oJson:UpdateString("unusedPlaceholders", "blank")
nSuccess := oPdf:TextReplace("apply", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "blank: applied = " + Str(oJsonOut:BoolOf("applied"))
nSuccess := oPdf:WriteFile("qa_output/invoice_no_po.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()