Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL i
LOCAL lnCount_i
LOCAL loJsonRead
LOCAL loJsonText

lnSuccess = 0

*  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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/invoice_labels.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

*  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.
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("labels[0].label","Invoice Number:")
loJson.UpdateString("labels[0].value","INV-2026-0500")
loJson.UpdateString("labels[1].label","Invoice Date:")
loJson.UpdateString("labels[1].value","April 2, 2026")
loJson.UpdateString("labels[2].label","customer")
loJson.UpdateString("labels[2].value","Contoso Ltd")
loJson.UpdateString("labels[3].label","Total Due:")
loJson.UpdateString("labels[3].value","$1,999.99")

loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.TextReplace("apply",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? "Not applied: " + loJsonOut.Emit()
    IF (lnSuccess = 0) THEN
        ? loPdf.LastErrorText
        RELEASE loPdf
        RELEASE loJson
        RELEASE loJsonOut
        CANCEL
    ENDIF

ENDIF

i = 0
lnCount_i = loJsonOut.SizeOfArray("replacements")
DO WHILE i < lnCount_i
    loJsonOut.I = i
    ? 'Replaced the value of "' + loJsonOut.StringOf("replacements[i].label") + '" on page ' + loJsonOut.StringOf("replacements[i].pages")
    i = i + 1
ENDDO

lnSuccess = loPdf.WriteFile("qa_output/invoice_reissued.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  Read the new values back with ExtractText's label option.
loJsonRead = CreateObject('Chilkat.JsonObject')
loJsonText = CreateObject('Chilkat.JsonObject')
loJsonRead.UpdateString("label","Total Due")
loJsonRead.UpdateString("labelValue","right")
lnSuccess = loPdf.ExtractText("text",loJsonRead,loJsonText)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loJsonRead
    RELEASE loJsonText
    CANCEL
ENDIF

? "Total Due is now: " + loJsonText.StringOf("text")

*  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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loJsonRead
RELEASE loJsonText