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

Fill a PDF Template: Replace {{placeholders}} with Values

See more PDF Text Replacement Examples

Demonstrates the TextReplace method on an invoice template whose values are written as placeholders such as {{customer}} and {{total}}. The "placeholders" object names each placeholder and the text to put in; every occurrence on the page is replaced, including a placeholder inside a sentence or inside a label such as "Tax ({{taxRate}}):". The text is redrawn with the font the template used, so the result looks like the template, and a value that is right-aligned in the template stays right-aligned. The changes are written by WriteFile.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Integer i
    Integer iCount_i
    String sTemp1
    String sTemp2
    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

    //  The template has these placeholders in its page text:
    //    {{customer}} (twice: "Bill To:" and the closing sentence), {{date}}, {{po}},
    //    two item rows: {{qty1}} {{desc1}} {{price1}} {{amount1}} and {{qty2}} {{desc2}} {{price2}} {{amount2}},
    //    {{subtotal}}, "Tax ({{taxRate}}):" with {{tax}}, and {{total}}.
    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" "2" 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" "$920.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.qty2" "6" 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" "$75.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.subtotal" "$995.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.taxRate" "7.35%" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.tax" "$73.13" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.total" "$1,068.13" To iSuccess

    //  "apply" makes every replacement, or none: if any value cannot be drawn with the
    //  template's font, the method returns false, the PDF is unchanged, and jsonOut says why.
    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

    //  One entry per placeholder: the pages it was found on and how many times it was replaced.
    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "replacements" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComStringOf Of hoJsonOut "replacements[i].field" To sTemp1
        Get ComIntOf Of hoJsonOut "replacements[i].count" To iTemp1
        Get ComStringOf Of hoJsonOut "replacements[i].pages" To sTemp2
        Showln sTemp1 ": " iTemp1 " on page(s) " sTemp2
        Move (i + 1) To i
    Loop

    //  The replacements stay in memory until the PDF is written.
    Get ComWriteFile Of hoPdf "qa_output/invoice_filled.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Wrote qa_output/invoice_filled.pdf"

    //  Sample output:

    //  customer: 2 on page(s) 1
    //  date: 1 on page(s) 1
    //  po: 1 on page(s) 1
    //  qty1: 1 on page(s) 1
    //  desc1: 1 on page(s) 1
    //  price1: 1 on page(s) 1
    //  amount1: 1 on page(s) 1
    //  qty2: 1 on page(s) 1
    //  desc2: 1 on page(s) 1
    //  price2: 1 on page(s) 1
    //  amount2: 1 on page(s) 1
    //  subtotal: 1 on page(s) 1
    //  taxRate: 1 on page(s) 1
    //  tax: 1 on page(s) 1
    //  total: 1 on page(s) 1
    //  Wrote qa_output/invoice_filled.pdf


End_Procedure