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

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

Chilkat Visual FoxPro Downloads

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

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

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

loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("placeholders.customer","Acme Corp")
*  (The other placeholders are left as they are for this example.)
loJson.UpdateString("unusedPlaceholders","keep")

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

? "In the page content, customer was replaced " + STR(loJsonOut.IntOf("replacements[0].count")) + " times."

*  Where else the placeholder occurs, untouched:
i = 0
lnCount_i = loJsonOut.SizeOfArray("notReplaced")
DO WHILE i < lnCount_i
    loJsonOut.I = i
    ? "not replaced: {{" + loJsonOut.StringOf("notReplaced[i].field") + "}} in " + loJsonOut.StringOf("notReplaced[i].where")
    i = i + 1
ENDDO

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

*  Sample output:

*  In the page content, customer was replaced 2 times.
*  not replaced: {{customer}} in metadata
*  not replaced: {{customer}} in linkTarget

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut