Unicode C Requires Chilkat v11.7.0+
Unicode C
Fill a PDF Template: Replace {{placeholders}} with Values
See more PDF Text Replacement Examples
Demonstrates the TextReplace method on an invoice template whose values are written as placeholders such as {{customer}} and {{total}}. The "placeholders" object names each placeholder and the text to put in; every occurrence on the page is replaced, including a placeholder inside a sentence or inside a label such as "Tax ({{taxRate}}):". The text is redrawn with the font the template used, so the result looks like the template, and a value that is right-aligned in the template stays right-aligned. The changes are written by WriteFile.Chilkat Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW 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 invoice_template.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/invoice_template.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
// The template has these placeholders in its page text:
// {{customer}} (twice: "Bill To:" and the closing sentence), {{date}}, {{po}},
// two item rows: {{qty1}} {{desc1}} {{price1}} {{amount1}} and {{qty2}} {{desc2}} {{price2}} {{amount2}},
// {{subtotal}}, "Tax ({{taxRate}}):" with {{tax}}, and {{total}}.
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"placeholders.customer",L"Acme Corp");
CkJsonObjectW_UpdateString(json,L"placeholders.date",L"2026-10-18");
CkJsonObjectW_UpdateString(json,L"placeholders.po",L"PO-88213");
CkJsonObjectW_UpdateString(json,L"placeholders.qty1",L"2");
CkJsonObjectW_UpdateString(json,L"placeholders.desc1",L"Cordless drill, 18 V");
CkJsonObjectW_UpdateString(json,L"placeholders.price1",L"$460.00");
CkJsonObjectW_UpdateString(json,L"placeholders.amount1",L"$920.00");
CkJsonObjectW_UpdateString(json,L"placeholders.qty2",L"6");
CkJsonObjectW_UpdateString(json,L"placeholders.desc2",L"Galvanized hinge, 4 inch");
CkJsonObjectW_UpdateString(json,L"placeholders.price2",L"$12.50");
CkJsonObjectW_UpdateString(json,L"placeholders.amount2",L"$75.00");
CkJsonObjectW_UpdateString(json,L"placeholders.subtotal",L"$995.00");
CkJsonObjectW_UpdateString(json,L"placeholders.taxRate",L"7.35%");
CkJsonObjectW_UpdateString(json,L"placeholders.tax",L"$73.13");
CkJsonObjectW_UpdateString(json,L"placeholders.total",L"$1,068.13");
// "apply" makes every replacement, or none: if any value cannot be drawn with the
// template's font, the method returns false, the PDF is unchanged, and jsonOut says why.
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
if (success == FALSE) {
wprintf(L"Not applied: %s\n",CkJsonObjectW_emit(jsonOut));
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
}
// One entry per placeholder: the pages it was found on and how many times it was replaced.
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"replacements");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"%s: %d on page(s) %s\n",CkJsonObjectW_stringOf(jsonOut,L"replacements[i].field"),CkJsonObjectW_IntOf(jsonOut,L"replacements[i].count"),CkJsonObjectW_stringOf(jsonOut,L"replacements[i].pages"));
i = i + 1;
}
// The replacements stay in memory until the PDF is written.
success = CkPdfW_WriteFile(pdf,L"qa_output/invoice_filled.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Wrote qa_output/invoice_filled.pdf\n");
// Sample output:
// customer: 2 on page(s) 1
// date: 1 on page(s) 1
// po: 1 on page(s) 1
// qty1: 1 on page(s) 1
// desc1: 1 on page(s) 1
// price1: 1 on page(s) 1
// amount1: 1 on page(s) 1
// qty2: 1 on page(s) 1
// desc2: 1 on page(s) 1
// price2: 1 on page(s) 1
// amount2: 1 on page(s) 1
// subtotal: 1 on page(s) 1
// taxRate: 1 on page(s) 1
// tax: 1 on page(s) 1
// total: 1 on page(s) 1
// Wrote qa_output/invoice_filled.pdf
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}