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

Check a PDF Text Replacement Before Applying It

See more PDF Text Replacement Examples

Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.

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
    String sTemp3
    Integer iTemp1
    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 most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
    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.subtotal" "$920.00" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.taxRate" "7.35%" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.tax" "$67.62" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.total" "$987.62" To iSuccess

    //  "check" reports what "apply" would do, without changing the PDF.
    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 "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 "ok: " bTemp1
    Get ComSizeOfArray Of hoJsonOut "replacements" To iTemp1
    Showln "would replace: " iTemp1 " placeholders"

    //  Each problem names the placeholder (field) or label, and a reason:
    //    notFound - the placeholder or label is not on the selected pages
    //    unusedPlaceholder - a placeholder on the page was given no value
    //    missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
    //    placeholderSpansFonts - the text is drawn with more than one font or size
    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "problems" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComStringOf Of hoJsonOut "problems[i].field" To sTemp1
        Get ComStringOf Of hoJsonOut "problems[i].label" To sTemp2
        Get ComStringOf Of hoJsonOut "problems[i].reason" To sTemp3
        Get ComIntOf Of hoJsonOut "problems[i].page" To iTemp1
        Showln "problem: " sTemp1 sTemp2 " - " sTemp3 " (page " iTemp1 ")"
        Move (i + 1) To i
    Loop

    //  Give the missing placeholders their values and check again.
    Get ComUpdateString Of hoJson "placeholders.po" "PO-88213" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.qty2" "0" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.desc2" "(no second item)" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.price2" "" To iSuccess
    Get ComUpdateString Of hoJson "placeholders.amount2" "" 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 "ok after adding the values: " bTemp1

    //  Now apply.
    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    If (bTemp1 = True) Begin
        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 "applied: " bTemp1
        Get ComWriteFile Of hoPdf "qa_output/invoice_checked.pdf" To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoPdf To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    //  Sample output:

    //  ok: false
    //  would replace: 10 placeholders
    //  problem: po - unusedPlaceholder (page 1)
    //  problem: qty2 - unusedPlaceholder (page 1)
    //  problem: desc2 - unusedPlaceholder (page 1)
    //  problem: price2 - unusedPlaceholder (page 1)
    //  problem: amount2 - unusedPlaceholder (page 1)
    //  ok after adding the values: true
    //  applied: true


End_Procedure