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

C
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    int i;
    int count_i;

    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

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/letter_template.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        return;
    }

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

    jsonOut = CkJsonObject_Create();
    success = CkPdf_TextReplace(pdf,"apply",json,jsonOut);
    if (success == FALSE) {
        printf("Not applied: %s\n",CkJsonObject_emit(jsonOut));
        if (success == FALSE) {
            printf("%s\n",CkPdf_lastErrorText(pdf));
            CkPdf_Dispose(pdf);
            CkJsonObject_Dispose(json);
            CkJsonObject_Dispose(jsonOut);
            return;
        }

    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jsonOut,"replacements");
    while (i < count_i) {
        CkJsonObject_putI(jsonOut,i);
        printf("[[%s]] replaced\n",CkJsonObject_stringOf(jsonOut,"replacements[i].field"));
        i = i + 1;
    }

    success = CkPdf_WriteFile(pdf,"qa_output/letter_rivera.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  Sample output:

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


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);

    }