PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
Alignment of Replaced PDF Text: Right-Aligned Amounts
See more PDF Text Replacement Examples
When the new text is wider or narrower than the text it replaces, the "align" option decides where it sits. The default, "auto", keeps a placeholder in place when it is right-aligned with other text on the page or centered, and otherwise keeps its left edge; a label's value is always left-aligned after its label. The invoice template's amounts are right-aligned, so the new amounts keep their right edge, which the "grid" text layout (characters placed by position) shows.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_JsonText
oleobject loo_SbText
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")
loo_Json.UpdateString("placeholders.date","2026-10-18")
loo_Json.UpdateString("placeholders.po","PO-88213")
loo_Json.UpdateString("placeholders.qty1","12")
loo_Json.UpdateString("placeholders.desc1","Cordless drill, 18 V")
loo_Json.UpdateString("placeholders.price1","$460.00")
loo_Json.UpdateString("placeholders.amount1","$5,520.00")
loo_Json.UpdateString("placeholders.qty2","60")
loo_Json.UpdateString("placeholders.desc2","Galvanized hinge, 4 inch")
loo_Json.UpdateString("placeholders.price2","$12.50")
loo_Json.UpdateString("placeholders.amount2","$750.00")
loo_Json.UpdateString("placeholders.subtotal","$6,270.00")
loo_Json.UpdateString("placeholders.taxRate","7.35%")
loo_Json.UpdateString("placeholders.tax","$460.85")
loo_Json.UpdateString("placeholders.total","$6,730.85")
// "auto" is the default: a placeholder that is right-aligned with other text keeps its right edge.
// The other choices are "left", "right" and "center".
loo_Json.UpdateString("align","auto")
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
// A replacement that runs into neighbouring text or off the page is still made, with a warning.
Write-Debug "warnings: " + string(loo_JsonOut.SizeOfArray("warnings"))
li_Success = loo_Pdf.WriteFile("qa_output/invoice_aligned.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The "grid" layout places each character by its position on the page, so the right-aligned
// amounts can be seen to line up (the wider amounts grew to the left).
loo_JsonText = create oleobject
li_rc = loo_JsonText.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonText.UpdateString("layout","grid")
loo_SbText = create oleobject
li_rc = loo_SbText.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Pdf.ExtractTextSb("text",loo_JsonText,loo_SbText,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_JsonText
destroy loo_SbText
return
end if
Write-Debug loo_SbText.GetAsString()
// Sample output:
// warnings: 0
// INVOICE
// Chilkat Hardware Supply
// 1200 Harbor Road, Suite 4
// Portside, WA 98000
//
//
// Bill To: Acme Corp
// Invoice #: INV-0000
// Date: 2026-10-18
// PO Number: PO-88213
// Ref: A-1
//
//
//
// Qty Description Unit Price Amount
// 12 Cordless drill, 18 V $460.00 $5,520.00
// 60 Galvanized hinge, 4 inch $12.50 $750.00
//
// Subtotal: $6,270.00
// Tax (7.35%): $460.85
// Total Due: $6,730.85
//
// Ref: A-2
//
// Payment is due within 30 days. Please include the invoice number with your payment.
// Thank you for your business, Acme Corp!
// View this invoice online
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_JsonText
destroy loo_SbText