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 <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;
}
CkJsonObjectW json;
json.UpdateString(L"placeholders.customer",L"Acme Corp");
// (The other placeholders are left as they are for this example.)
json.UpdateString(L"unusedPlaceholders",L"keep");
CkJsonObjectW jsonOut;
success = pdf.TextReplace(L"apply",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"In the page content, customer was replaced %d times.\n",jsonOut.IntOf(L"replacements[0].count"));
// Where else the placeholder occurs, untouched:
int i = 0;
int count_i = jsonOut.SizeOfArray(L"notReplaced");
while (i < count_i) {
jsonOut.put_I(i);
wprintf(L"not replaced: {{%s}} in %s\n",jsonOut.stringOf(L"notReplaced[i].field"),jsonOut.stringOf(L"notReplaced[i].where"));
i = i + 1;
}
success = pdf.WriteFile(L"qa_output/invoice_customer.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// Sample output:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget
}