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 <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
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;
}
// Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
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.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");
jsonOut = CkJsonObjectW_Create();
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"error (default): ok = %d, problem: %s %s\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"),CkJsonObjectW_stringOf(jsonOut,L"problems[0].field"),CkJsonObjectW_stringOf(jsonOut,L"problems[0].reason"));
// "keep": {{po}} stays on the page, literally.
CkJsonObjectW_UpdateString(json,L"unusedPlaceholders",L"keep");
success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"keep: ok = %d\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"));
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
CkJsonObjectW_UpdateString(json,L"unusedPlaceholders",L"blank");
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"blank: applied = %d\n",CkJsonObjectW_BoolOf(jsonOut,L"applied"));
success = CkPdfW_WriteFile(pdf,L"qa_output/invoice_no_po.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}