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

Unicode C++
#include <CkPdfW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  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

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/letter_template.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

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

    CkJsonObjectW jsonOut;
    success = pdf.TextReplace(L"apply",json,jsonOut);
    if (success == false) {
        wprintf(L"Not applied: %s\n",jsonOut.emit());
        if (success == false) {
            wprintf(L"%s\n",pdf.lastErrorText());
            return;
        }

    }

    int i = 0;
    int count_i = jsonOut.SizeOfArray(L"replacements");
    while (i < count_i) {
        jsonOut.put_I(i);
        wprintf(L"[[%s]] replaced\n",jsonOut.stringOf(L"replacements[i].field"));
        i = i + 1;
    }

    success = pdf.WriteFile(L"qa_output/letter_rivera.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  Sample output:

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