PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("placeholders.customer","Acme Corp")
// (The other placeholders are left as they are for this example.)
loo_Json.UpdateString("unusedPlaceholders","keep")
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 loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
Write-Debug "In the page content, customer was replaced " + string(loo_JsonOut.IntOf("replacements[0].count")) + " times."
// Where else the placeholder occurs, untouched:
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("notReplaced")
do while i < li_Count_i
loo_JsonOut.I = i
Write-Debug "not replaced: {{" + loo_JsonOut.StringOf("notReplaced[i].field") + "}} in " + loo_JsonOut.StringOf("notReplaced[i].where")
i = i + 1
loop
li_Success = loo_Pdf.WriteFile("qa_output/invoice_customer.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:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut