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

PDF Placeholders with Other Delimiters: [[name]]

See more PDF Text Replacement Examples

Demonstrates the "placeholderSyntax" option. Placeholders are {{name}} by default; a template written with other delimiters, such as [[name]] or <>, says so with placeholderSyntax, written around the word "name".

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

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

//  The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("placeholderSyntax", "[[name]]")
oJson:UpdateString("placeholders.date", "October 18, 2026")
oJson:UpdateString("placeholders.name", "Ms. Rivera")
oJson:UpdateString("placeholders.expiry", "December 31, 2026")
oJson:UpdateString("placeholders.amount", "$299.00")
oJson:UpdateString("placeholders.agent", "Dana Whitfield, Account Manager")

oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:TextReplace("apply", oJson, oJsonOut)
IF (nSuccess == 0)
    ? "Not applied: " + oJsonOut:Emit()
    IF (nSuccess == 0)
        ? oPdf:LastErrorText
        oPdf:destroy()
        oJson:destroy()
        oJsonOut:destroy()
        RETURN
    ENDIF

ENDIF

i := 0
nCount_i := oJsonOut:SizeOfArray("replacements")
DO WHILE i < nCount_i
    oJsonOut:I := i
    ? "[[" + oJsonOut:StringOf("replacements[i].field") + "]] replaced"
    i := i + 1
ENDDO

nSuccess := oPdf:WriteFile("qa_output/letter_rivera.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  Sample output:

//  [[date]] replaced
//  [[name]] replaced
//  [[expiry]] replaced
//  [[amount]] replaced
//  [[agent]] replaced

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