Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/invoice_template.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

//  Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("placeholders.customer","Acme Corp")
loo_Json.UpdateString("placeholders.date","2026-10-18")
loo_Json.UpdateString("placeholders.qty1","2")
loo_Json.UpdateString("placeholders.desc1","Cordless drill, 18 V")
loo_Json.UpdateString("placeholders.price1","$460.00")
loo_Json.UpdateString("placeholders.amount1","$920.00")
loo_Json.UpdateString("placeholders.qty2","6")
loo_Json.UpdateString("placeholders.desc2","Galvanized hinge, 4 inch")
loo_Json.UpdateString("placeholders.price2","$12.50")
loo_Json.UpdateString("placeholders.amount2","$75.00")
loo_Json.UpdateString("placeholders.subtotal","$995.00")
loo_Json.UpdateString("placeholders.taxRate","7.35%")
loo_Json.UpdateString("placeholders.tax","$73.13")
loo_Json.UpdateString("placeholders.total","$1,068.13")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "error (default): ok = " + string(loo_JsonOut.BoolOf("ok")) + ", problem: " + loo_JsonOut.StringOf("problems[0].field") + " " + loo_JsonOut.StringOf("problems[0].reason")

//  "keep": {{po}} stays on the page, literally.
loo_Json.UpdateString("unusedPlaceholders","keep")
li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "keep: ok = " + string(loo_JsonOut.BoolOf("ok"))

//  "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
loo_Json.UpdateString("unusedPlaceholders","blank")
li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "blank: applied = " + string(loo_JsonOut.BoolOf("applied"))

li_Success = loo_Pdf.WriteFile("qa_output/invoice_no_po.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  Sample output:

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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut