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

PDF Placeholders Without a Value: Error, Blank or Keep

See more PDF Text Replacement Examples

Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1
    String sTemp2
    Boolean bTemp1

    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

    //  Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
    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.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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Get ComStringOf Of hoJsonOut "problems[0].field" To sTemp1
    Get ComStringOf Of hoJsonOut "problems[0].reason" To sTemp2
    Showln "error (default): ok = " bTemp1 ", problem: " sTemp1 " " sTemp2

    //  "keep": {{po}} stays on the page, literally.
    Get ComUpdateString Of hoJson "unusedPlaceholders" "keep" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Showln "keep: ok = " bTemp1

    //  "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
    Get ComUpdateString Of hoJson "unusedPlaceholders" "blank" To iSuccess
    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 ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "applied" To bTemp1
    Showln "blank: applied = " bTemp1

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

    //  Sample output:

    //  error (default): ok = false, problem: po unusedPlaceholder
    //  keep: ok = true
    //  blank: applied = true


End_Procedure