Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
PDF Placeholders Without a Value: Error, Blank or Keep
See more PDF Text Replacement Examples
Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.Chilkat Unicode C++ Downloads
#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 invoice_template.pdf
CkPdfW pdf;
success = pdf.LoadFile(L"qa_data/pdf/invoice_template.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
CkJsonObjectW json;
json.UpdateString(L"placeholders.customer",L"Acme Corp");
json.UpdateString(L"placeholders.date",L"2026-10-18");
json.UpdateString(L"placeholders.qty1",L"2");
json.UpdateString(L"placeholders.desc1",L"Cordless drill, 18 V");
json.UpdateString(L"placeholders.price1",L"$460.00");
json.UpdateString(L"placeholders.amount1",L"$920.00");
json.UpdateString(L"placeholders.qty2",L"6");
json.UpdateString(L"placeholders.desc2",L"Galvanized hinge, 4 inch");
json.UpdateString(L"placeholders.price2",L"$12.50");
json.UpdateString(L"placeholders.amount2",L"$75.00");
json.UpdateString(L"placeholders.subtotal",L"$995.00");
json.UpdateString(L"placeholders.taxRate",L"7.35%");
json.UpdateString(L"placeholders.tax",L"$73.13");
json.UpdateString(L"placeholders.total",L"$1,068.13");
CkJsonObjectW jsonOut;
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
success = pdf.TextReplace(L"check",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"error (default): ok = %d, problem: %s %s\n",jsonOut.BoolOf(L"ok"),jsonOut.stringOf(L"problems[0].field"),jsonOut.stringOf(L"problems[0].reason"));
// "keep": {{po}} stays on the page, literally.
json.UpdateString(L"unusedPlaceholders",L"keep");
success = pdf.TextReplace(L"check",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"keep: ok = %d\n",jsonOut.BoolOf(L"ok"));
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
json.UpdateString(L"unusedPlaceholders",L"blank");
success = pdf.TextReplace(L"apply",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"blank: applied = %d\n",jsonOut.BoolOf(L"applied"));
success = pdf.WriteFile(L"qa_output/invoice_no_po.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true
}