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

Replace the Value After a Label in a PDF (Re-issue an Invoice)

See more PDF Text Replacement Examples

Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.

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
    Variant vJson
Read    Handle hoJsonRead
    Variant vJsonText
    Handle hoJsonText
    String sTemp1
    String sTemp2

    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

    //  The invoice has lines such as "Invoice Number:   INV-2026-0412" and "Total Due:  $1,234.53".
    //  Each label's new value replaces whatever follows the label on its line.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    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" "Invoice Date:" To iSuccess
    Get ComUpdateString Of hoJson "labels[1].value" "April 2, 2026" To iSuccess
    Get ComUpdateString Of hoJson "labels[2].label" "customer" To iSuccess
    Get ComUpdateString Of hoJson "labels[2].value" "Contoso Ltd" To iSuccess
    Get ComUpdateString Of hoJson "labels[3].label" "Total Due:" To iSuccess
    Get ComUpdateString Of hoJson "labels[3].value" "$1,999.99" 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 ComEmit Of hoJsonOut To sTemp1
        Showln "Not applied: " sTemp1
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoPdf To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "replacements" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComStringOf Of hoJsonOut "replacements[i].label" To sTemp1
        Get ComStringOf Of hoJsonOut "replacements[i].pages" To sTemp2
        Showln 'Replaced the value of "' sTemp1 '" on page ' sTemp2
        Move (i + 1) To i
    Loop

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

    //  Read the new values back with ExtractText's label option.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonRead
    If (Not(IsComObjectCreated(hoJsonRead))) Begin
        Send CreateComObject of hoJsonRead
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonText
    If (Not(IsComObjectCreated(hoJsonText))) Begin
        Send CreateComObject of hoJsonText
    End
    Get ComUpdateString Of hoJsonRead "label" "Total Due" To iSuccess
    Get ComUpdateString Of hoJsonRead "labelValue" "right" To iSuccess
    Get pvComObject of hoJsonRead to vJsonRead
    Get pvComObject of hoJsonText to vJsonText
    Get ComExtractText Of hoPdf "text" vJsonRead vJsonText To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonText "text" To sTemp1
    Showln "Total Due is now: " sTemp1

    //  Sample output:

    //  Replaced the value of "Invoice Number:" on page 1
    //  Replaced the value of "Invoice Date:" on page 1
    //  Replaced the value of "customer" on page 1
    //  Replaced the value of "Total Due:" on page 1
    //  Total Due is now: $1,999.99


End_Procedure