PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPdf.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_template.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"placeholders.customer","Acme Corp")
CkJsonObject::ckUpdateString(json,"placeholders.date","2026-10-18")
CkJsonObject::ckUpdateString(json,"placeholders.po","PO-88213")
CkJsonObject::ckUpdateString(json,"placeholders.qty1","12")
CkJsonObject::ckUpdateString(json,"placeholders.desc1","Cordless drill, 18 V")
CkJsonObject::ckUpdateString(json,"placeholders.price1","$460.00")
CkJsonObject::ckUpdateString(json,"placeholders.amount1","$5,520.00")
CkJsonObject::ckUpdateString(json,"placeholders.qty2","60")
CkJsonObject::ckUpdateString(json,"placeholders.desc2","Galvanized hinge, 4 inch")
CkJsonObject::ckUpdateString(json,"placeholders.price2","$12.50")
CkJsonObject::ckUpdateString(json,"placeholders.amount2","$750.00")
CkJsonObject::ckUpdateString(json,"placeholders.subtotal","$6,270.00")
CkJsonObject::ckUpdateString(json,"placeholders.taxRate","7.35%")
CkJsonObject::ckUpdateString(json,"placeholders.tax","$460.85")
CkJsonObject::ckUpdateString(json,"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".
CkJsonObject::ckUpdateString(json,"align","auto")
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
If success = 0
Debug "Not applied: " + CkJsonObject::ckEmit(jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
EndIf
; A replacement that runs into neighbouring text or off the page is still made, with a warning.
Debug "warnings: " + Str(CkJsonObject::ckSizeOfArray(jsonOut,"warnings"))
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_aligned.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; 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).
jsonText.i = CkJsonObject::ckCreate()
If jsonText.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(jsonText,"layout","grid")
sbText.i = CkStringBuilder::ckCreate()
If sbText.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckExtractTextSb(pdf,"text",jsonText,sbText,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(jsonText)
CkStringBuilder::ckDispose(sbText)
ProcedureReturn
EndIf
Debug CkStringBuilder::ckGetAsString(sbText)
; 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
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(jsonText)
CkStringBuilder::ckDispose(sbText)
ProcedureReturn
EndProcedure