PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
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
// Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
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.subtotal","$920.00")
loo_Json.UpdateString("placeholders.taxRate","7.35%")
loo_Json.UpdateString("placeholders.tax","$67.62")
loo_Json.UpdateString("placeholders.total","$987.62")
// "check" reports what "apply" would do, without changing the PDF.
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
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 "ok: " + string(loo_JsonOut.BoolOf("ok"))
Write-Debug "would replace: " + string(loo_JsonOut.SizeOfArray("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
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("problems")
do while i < li_Count_i
loo_JsonOut.I = i
Write-Debug "problem: " + loo_JsonOut.StringOf("problems[i].field") + loo_JsonOut.StringOf("problems[i].label") + " - " + loo_JsonOut.StringOf("problems[i].reason") + " (page " + string(loo_JsonOut.IntOf("problems[i].page")) + ")"
i = i + 1
loop
// Give the missing placeholders their values and check again.
loo_Json.UpdateString("placeholders.po","PO-88213")
loo_Json.UpdateString("placeholders.qty2","0")
loo_Json.UpdateString("placeholders.desc2","(no second item)")
loo_Json.UpdateString("placeholders.price2","")
loo_Json.UpdateString("placeholders.amount2","")
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 "ok after adding the values: " + string(loo_JsonOut.BoolOf("ok"))
// Now apply.
if loo_JsonOut.BoolOf("ok") = 1 then
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 "applied: " + string(loo_JsonOut.BoolOf("applied"))
li_Success = loo_Pdf.WriteFile("qa_output/invoice_checked.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
end if
// 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
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut