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

Fill a PDF Template: Replace {{placeholders}} with Values

See more PDF Text Replacement Examples

Demonstrates the TextReplace method on an invoice template whose values are written as placeholders such as {{customer}} and {{total}}. The "placeholders" object names each placeholder and the text to put in; every occurrence on the page is replaced, including a placeholder inside a sentence or inside a label such as "Tax ({{taxRate}}):". The text is redrawn with the font the template used, so the result looks like the template, and a value that is right-aligned in the template stays right-aligned. The changes are written by WriteFile.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
long i
long li_Count_i

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

//  The template has these placeholders in its page text:
//    {{customer}} (twice: "Bill To:" and the closing sentence), {{date}}, {{po}},
//    two item rows: {{qty1}} {{desc1}} {{price1}} {{amount1}} and {{qty2}} {{desc2}} {{price2}} {{amount2}},
//    {{subtotal}}, "Tax ({{taxRate}}):" with {{tax}}, and {{total}}.
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.po","PO-88213")
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")

//  "apply" makes every replacement, or none: if any value cannot be drawn with the
//  template's font, the method returns false, the PDF is unchanged, and jsonOut says why.
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug "Not applied: " + loo_JsonOut.Emit()
    if li_Success = 0 then
        Write-Debug loo_Pdf.LastErrorText
        destroy loo_Pdf
        destroy loo_Json
        destroy loo_JsonOut
        return
    end if

end if

//  One entry per placeholder: the pages it was found on and how many times it was replaced.
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("replacements")
do while i < li_Count_i
    loo_JsonOut.I = i
    Write-Debug loo_JsonOut.StringOf("replacements[i].field") + ": " + string(loo_JsonOut.IntOf("replacements[i].count")) + " on page(s) " + loo_JsonOut.StringOf("replacements[i].pages")
    i = i + 1
loop

//  The replacements stay in memory until the PDF is written.
li_Success = loo_Pdf.WriteFile("qa_output/invoice_filled.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Wrote qa_output/invoice_filled.pdf"

//  Sample output:

//  customer: 2 on page(s) 1
//  date: 1 on page(s) 1
//  po: 1 on page(s) 1
//  qty1: 1 on page(s) 1
//  desc1: 1 on page(s) 1
//  price1: 1 on page(s) 1
//  amount1: 1 on page(s) 1
//  qty2: 1 on page(s) 1
//  desc2: 1 on page(s) 1
//  price2: 1 on page(s) 1
//  amount2: 1 on page(s) 1
//  subtotal: 1 on page(s) 1
//  taxRate: 1 on page(s) 1
//  tax: 1 on page(s) 1
//  total: 1 on page(s) 1
//  Wrote qa_output/invoice_filled.pdf


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut