Unicode C Requires Chilkat v11.7.0+
Unicode C
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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;
}
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"placeholders.customer",L"Acme Corp");
// (The other placeholders are left as they are for this example.)
CkJsonObjectW_UpdateString(json,L"unusedPlaceholders",L"keep");
jsonOut = CkJsonObjectW_Create();
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"In the page content, customer was replaced %d times.\n",CkJsonObjectW_IntOf(jsonOut,L"replacements[0].count"));
// Where else the placeholder occurs, untouched:
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"notReplaced");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"not replaced: {{%s}} in %s\n",CkJsonObjectW_stringOf(jsonOut,L"notReplaced[i].field"),CkJsonObjectW_stringOf(jsonOut,L"notReplaced[i].where"));
i = i + 1;
}
success = CkPdfW_WriteFile(pdf,L"qa_output/invoice_customer.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Sample output:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}