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

Check a PDF Text Replacement Before Applying It

See more PDF Text Replacement Examples

Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL i
LOCAL nCount_i

nSuccess := 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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/invoice_template.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

//  Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("placeholders.customer", "Acme Corp")
oJson:UpdateString("placeholders.date", "2026-10-18")
oJson:UpdateString("placeholders.qty1", "2")
oJson:UpdateString("placeholders.desc1", "Cordless drill, 18 V")
oJson:UpdateString("placeholders.price1", "$460.00")
oJson:UpdateString("placeholders.amount1", "$920.00")
oJson:UpdateString("placeholders.subtotal", "$920.00")
oJson:UpdateString("placeholders.taxRate", "7.35%")
oJson:UpdateString("placeholders.tax", "$67.62")
oJson:UpdateString("placeholders.total", "$987.62")

//  "check" reports what "apply" would do, without changing the PDF.
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:TextReplace("check", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "ok: " + Str(oJsonOut:BoolOf("ok"))
? "would replace: " + Str(oJsonOut:SizeOfArray("replacements")) + " placeholders"

//  Each problem names the placeholder (field) or label, and a reason:
//    notFound - the placeholder or label is not on the selected pages
//    unusedPlaceholder - a placeholder on the page was given no value
//    missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
//    placeholderSpansFonts - the text is drawn with more than one font or size
i := 0
nCount_i := oJsonOut:SizeOfArray("problems")
DO WHILE i < nCount_i
    oJsonOut:I := i
    ? "problem: " + oJsonOut:StringOf("problems[i].field") + oJsonOut:StringOf("problems[i].label") + " - " + oJsonOut:StringOf("problems[i].reason") + " (page " + Str(oJsonOut:IntOf("problems[i].page")) + ")"
    i := i + 1
ENDDO

//  Give the missing placeholders their values and check again.
oJson:UpdateString("placeholders.po", "PO-88213")
oJson:UpdateString("placeholders.qty2", "0")
oJson:UpdateString("placeholders.desc2", "(no second item)")
oJson:UpdateString("placeholders.price2", "")
oJson:UpdateString("placeholders.amount2", "")
nSuccess := oPdf:TextReplace("check", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "ok after adding the values: " + Str(oJsonOut:BoolOf("ok"))

//  Now apply.
IF (oJsonOut:BoolOf("ok") == 1)
    nSuccess := oPdf:TextReplace("apply", oJson, oJsonOut)
    IF (nSuccess == 0)
        ? oPdf:LastErrorText
        oPdf:destroy()
        oJson:destroy()
        oJsonOut:destroy()
        RETURN
    ENDIF

    ? "applied: " + Str(oJsonOut:BoolOf("applied"))
    nSuccess := oPdf:WriteFile("qa_output/invoice_checked.pdf")
    IF (nSuccess == 0)
        ? oPdf:LastErrorText
        oPdf:destroy()
        oJson:destroy()
        oJsonOut:destroy()
        RETURN
    ENDIF

ENDIF

//  Sample output:

//  ok: false
//  would replace: 10 placeholders
//  problem: po - unusedPlaceholder (page 1)
//  problem: qty2 - unusedPlaceholder (page 1)
//  problem: desc2 - unusedPlaceholder (page 1)
//  problem: price2 - unusedPlaceholder (page 1)
//  problem: amount2 - unusedPlaceholder (page 1)
//  ok after adding the values: true
//  applied: true

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()