C Requires Chilkat v11.7.0+
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 C Downloads
#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 invoice_template.pdf
pdf = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/invoice_template.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_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 = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"placeholders.customer","Acme Corp");
CkJsonObject_UpdateString(json,"placeholders.date","2026-10-18");
CkJsonObject_UpdateString(json,"placeholders.po","PO-88213");
CkJsonObject_UpdateString(json,"placeholders.qty1","2");
CkJsonObject_UpdateString(json,"placeholders.desc1","Cordless drill, 18 V");
CkJsonObject_UpdateString(json,"placeholders.price1","$460.00");
CkJsonObject_UpdateString(json,"placeholders.amount1","$920.00");
CkJsonObject_UpdateString(json,"placeholders.qty2","6");
CkJsonObject_UpdateString(json,"placeholders.desc2","Galvanized hinge, 4 inch");
CkJsonObject_UpdateString(json,"placeholders.price2","$12.50");
CkJsonObject_UpdateString(json,"placeholders.amount2","$75.00");
CkJsonObject_UpdateString(json,"placeholders.subtotal","$995.00");
CkJsonObject_UpdateString(json,"placeholders.taxRate","7.35%");
CkJsonObject_UpdateString(json,"placeholders.tax","$73.13");
CkJsonObject_UpdateString(json,"placeholders.total","$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 = 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;
}
}
// One entry per placeholder: the pages it was found on and how many times it was replaced.
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonOut,"replacements");
while (i < count_i) {
CkJsonObject_putI(jsonOut,i);
printf("%s: %d on page(s) %s\n",CkJsonObject_stringOf(jsonOut,"replacements[i].field"),CkJsonObject_IntOf(jsonOut,"replacements[i].count"),CkJsonObject_stringOf(jsonOut,"replacements[i].pages"));
i = i + 1;
}
// The replacements stay in memory until the PDF is written.
success = CkPdf_WriteFile(pdf,"qa_output/invoice_filled.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("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
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}