C Requires Chilkat v11.7.0+
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 C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject 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 = 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;
}
// Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"placeholders.customer","Acme Corp");
CkJsonObject_UpdateString(json,"placeholders.date","2026-10-18");
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");
jsonOut = CkJsonObject_Create();
// The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
success = CkPdf_TextReplace(pdf,"check",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("error (default): ok = %d, problem: %s %s\n",CkJsonObject_BoolOf(jsonOut,"ok"),CkJsonObject_stringOf(jsonOut,"problems[0].field"),CkJsonObject_stringOf(jsonOut,"problems[0].reason"));
// "keep": {{po}} stays on the page, literally.
CkJsonObject_UpdateString(json,"unusedPlaceholders","keep");
success = CkPdf_TextReplace(pdf,"check",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("keep: ok = %d\n",CkJsonObject_BoolOf(jsonOut,"ok"));
// "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
CkJsonObject_UpdateString(json,"unusedPlaceholders","blank");
success = CkPdf_TextReplace(pdf,"apply",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("blank: applied = %d\n",CkJsonObject_BoolOf(jsonOut,"applied"));
success = CkPdf_WriteFile(pdf,"qa_output/invoice_no_po.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// Sample output:
// error (default): ok = false, problem: po unusedPlaceholder
// keep: ok = true
// blank: applied = true
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}