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

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

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
    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_labels.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_labels.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

    //  "Tracking Number:" is not on this invoice.
    Get ComUpdateString Of hoJson "labels[0].label" "Invoice Number:" To iSuccess
    Get ComUpdateString Of hoJson "labels[0].value" "INV-2026-0500" To iSuccess
    Get ComUpdateString Of hoJson "labels[1].label" "Tracking Number:" To iSuccess
    Get ComUpdateString Of hoJson "labels[1].value" "1Z999AA10123456784" 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
    Get ComStringOf Of hoJsonOut "problems[0].label" To sTemp1
    Get ComStringOf Of hoJsonOut "problems[0].reason" To sTemp2
    Showln "default: ok = " bTemp1 ', problem: "' sTemp1 '" ' sTemp2

    //  With "ignore", the label that is not there is skipped.
    Get ComUpdateString Of hoJson "unmatchedValues" "ignore" 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
    Get ComSizeOfArray Of hoJsonOut "replacements" To iTemp1
    Showln "ignore: applied = " bTemp1 ", replacements: " iTemp1

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

    //  Sample output:

    //  default: ok = false, problem: "Tracking Number:" notFound
    //  ignore: applied = true, replacements: 1


End_Procedure