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

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

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

    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
    //  (The other placeholders are left as they are for this example.)
    Get ComUpdateString Of hoJson "unusedPlaceholders" "keep" 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 ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "replacements[0].count" To iTemp1
    Showln "In the page content, customer was replaced " iTemp1 " times."

    //  Where else the placeholder occurs, untouched:
    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "notReplaced" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComStringOf Of hoJsonOut "notReplaced[i].field" To sTemp1
        Get ComStringOf Of hoJsonOut "notReplaced[i].where" To sTemp2
        Showln "not replaced: {{" sTemp1 "}} in " sTemp2
        Move (i + 1) To i
    Loop

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

    //  Sample output:

    //  In the page content, customer was replaced 2 times.
    //  not replaced: {{customer}} in metadata
    //  not replaced: {{customer}} in linkTarget


End_Procedure