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

Replace PDF Text on Selected Pages Only

See more PDF Text Replacement Examples

Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut

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 order_confirmation.pdf

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

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')

*  Both pages have "Status:   Pending".  With pages "2", only page 2 is searched.
loJson.UpdateString("pages","2")
loJson.UpdateString("labels[0].label","Status:")
loJson.UpdateString("labels[0].value","Shipped")
lnSuccess = loPdf.TextReplace("check",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "pages 2: found on page(s) " + loJsonOut.StringOf("replacements[0].pages")

*  With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
loJson.UpdateString("pages","all")
lnSuccess = loPdf.TextReplace("check",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "pages all, first occurrence: found on page(s) " + loJsonOut.StringOf("replacements[0].pages")

*  Every occurrence on every page.
loJson.UpdateString("labels[0].occurrence","all")
lnSuccess = loPdf.TextReplace("apply",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "pages all, every occurrence: " + STR(loJsonOut.IntOf("replacements[0].count")) + " replaced on page(s) " + loJsonOut.StringOf("replacements[0].pages")

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

*  Sample output:

*  pages 2: found on page(s) 2
*  pages all, first occurrence: found on page(s) 1
*  pages all, every occurrence: 2 replaced on page(s) 1,2

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut