PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Replace the Value After a Label in a PDF (Re-issue an Invoice)
See more PDF Text Replacement Examples
Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
long i
long li_Count_i
oleobject loo_JsonRead
oleobject loo_JsonText
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_labels.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_labels.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
// The invoice has lines such as "Invoice Number: INV-2026-0412" and "Total Due: $1,234.53".
// Each label's new value replaces whatever follows the label on its line.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateString("labels[0].label","Invoice Number:")
loo_Json.UpdateString("labels[0].value","INV-2026-0500")
loo_Json.UpdateString("labels[1].label","Invoice Date:")
loo_Json.UpdateString("labels[1].value","April 2, 2026")
loo_Json.UpdateString("labels[2].label","customer")
loo_Json.UpdateString("labels[2].value","Contoso Ltd")
loo_Json.UpdateString("labels[3].label","Total Due:")
loo_Json.UpdateString("labels[3].value","$1,999.99")
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
i = 0
li_Count_i = loo_JsonOut.SizeOfArray("replacements")
do while i < li_Count_i
loo_JsonOut.I = i
Write-Debug "Replaced the value of ~"" + loo_JsonOut.StringOf("replacements[i].label") + "~" on page " + loo_JsonOut.StringOf("replacements[i].pages")
i = i + 1
loop
li_Success = loo_Pdf.WriteFile("qa_output/invoice_reissued.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// Read the new values back with ExtractText's label option.
loo_JsonRead = create oleobject
li_rc = loo_JsonRead.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonText = create oleobject
li_rc = loo_JsonText.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonRead.UpdateString("label","Total Due")
loo_JsonRead.UpdateString("labelValue","right")
li_Success = loo_Pdf.ExtractText("text",loo_JsonRead,loo_JsonText)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_JsonRead
destroy loo_JsonText
return
end if
Write-Debug "Total Due is now: " + loo_JsonText.StringOf("text")
// Sample output:
// Replaced the value of "Invoice Number:" on page 1
// Replaced the value of "Invoice Date:" on page 1
// Replaced the value of "customer" on page 1
// Replaced the value of "Total Due:" on page 1
// Total Due is now: $1,999.99
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_JsonRead
destroy loo_JsonText