Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.7.0+

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vJsonText
    Handle hoJsonText
    Variant vSbText
    Handle hoSbText
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/invoice_template.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "placeholders.customer" "Acme Corp" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.date" "2026-10-18" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.po" "PO-88213" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.qty1" "12" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.desc1" "Cordless drill, 18 V" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.price1" "$460.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.amount1" "$5,520.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.qty2" "60" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.desc2" "Galvanized hinge, 4 inch" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.price2" "$12.50" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.amount2" "$750.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.subtotal" "$6,270.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.taxRate" "7.35%" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.tax" "$460.85" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.total" "$6,730.85" To iSuccess

    //  "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".
    Get ComUpdateString Of hoJson "align" "auto" To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "apply" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComEmit Of hoJsonOut To sTemp1
        Showln "Not applied: " sTemp1
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoPdf To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    //  A replacement that runs into neighbouring text or off the page is still made, with a warning.
    Get ComSizeOfArray Of hoJsonOut "warnings" To iTemp1
    Showln "warnings: " iTemp1

    Get ComWriteFile Of hoPdf "qa_output/invoice_aligned.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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).
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonText
    If (Not(IsComObjectCreated(hoJsonText))) Begin
        Send CreateComObject of hoJsonText
    End
    Get ComUpdateString Of hoJsonText "layout" "grid" To iSuccess
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbText
    If (Not(IsComObjectCreated(hoSbText))) Begin
        Send CreateComObject of hoSbText
    End
    Get pvComObject of hoJsonText to vJsonText
    Get pvComObject of hoSbText to vSbText
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractTextSb Of hoPdf "text" vJsonText vSbText vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbText To sTemp1
    Showln sTemp1

    //  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


End_Procedure