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

Replace the Second (or Every) Occurrence of a Label in a PDF

See more PDF Text Replacement Examples

Demonstrates the "occurrence" member of a label entry. By default the first occurrence of a label, in reading order across the selected pages, is replaced; "occurrence" can be a number for the nth, or "all" for every occurrence. The invoice template has "Ref:" twice.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    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 Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  This template's {{placeholders}} are not being filled here: "keep" leaves them as they are
    //  (the default, "error", would refuse to apply while a placeholder on the page has no value).
    Get ComUpdateString Of hoJson "unusedPlaceholders" "keep" To iSuccess

    //  "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
    //  The default replaces the first occurrence only.
    Get ComUpdateString Of hoJson "labels[0].label" "Ref:" To iSuccess
    Get ComUpdateString Of hoJson "labels[0].value" "B-7" 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 ComIntOf Of hoJsonOut "replacements[0].count" To iTemp1
    Showln "default: " iTemp1 " occurrence replaced"

    //  The second occurrence only.
    Get ComUpdateString Of hoJson "labels[0].occurrence" "2" 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 ComIntOf Of hoJsonOut "replacements[0].count" To iTemp1
    Showln "occurrence 2: " iTemp1 " occurrence replaced"

    //  Every occurrence.
    Get ComUpdateString Of hoJson "labels[0].occurrence" "all" 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 ComIntOf Of hoJsonOut "replacements[0].count" To iTemp1
    Showln "occurrence all: " iTemp1 " occurrences replaced"

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

    //  Sample output:

    //  default: 1 occurrence replaced
    //  occurrence 2: 1 occurrence replaced
    //  occurrence all: 2 occurrences replaced


End_Procedure