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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/letter_template.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    ;  The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"placeholderSyntax","[[name]]")
    CkJsonObject::ckUpdateString(json,"placeholders.date","October 18, 2026")
    CkJsonObject::ckUpdateString(json,"placeholders.name","Ms. Rivera")
    CkJsonObject::ckUpdateString(json,"placeholders.expiry","December 31, 2026")
    CkJsonObject::ckUpdateString(json,"placeholders.amount","$299.00")
    CkJsonObject::ckUpdateString(json,"placeholders.agent","Dana Whitfield, Account Manager")

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
    If success = 0
        Debug "Not applied: " + CkJsonObject::ckEmit(jsonOut)
        If success = 0
            Debug CkPdf::ckLastErrorText(pdf)
            CkPdf::ckDispose(pdf)
            CkJsonObject::ckDispose(json)
            CkJsonObject::ckDispose(jsonOut)
            ProcedureReturn
        EndIf

    EndIf

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"replacements")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "[[" + CkJsonObject::ckStringOf(jsonOut,"replacements[i].field") + "]] replaced"
        i = i + 1
    Wend

    success = CkPdf::ckWriteFile(pdf,"qa_output/letter_rivera.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  Sample output:

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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure