Unicode C Requires Chilkat v11.7.0+
Unicode C
Replace PDF Text on Selected Pages Only
See more PDF Text Replacement Examples
Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.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 order_confirmation.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/order_confirmation.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// Both pages have "Status: Pending". With pages "2", only page 2 is searched.
CkJsonObjectW_UpdateString(json,L"pages",L"2");
CkJsonObjectW_UpdateString(json,L"labels[0].label",L"Status:");
CkJsonObjectW_UpdateString(json,L"labels[0].value",L"Shipped");
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"pages 2: found on page(s) %s\n",CkJsonObjectW_stringOf(jsonOut,L"replacements[0].pages"));
// With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
CkJsonObjectW_UpdateString(json,L"pages",L"all");
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"pages all, first occurrence: found on page(s) %s\n",CkJsonObjectW_stringOf(jsonOut,L"replacements[0].pages"));
// Every occurrence on every page.
CkJsonObjectW_UpdateString(json,L"labels[0].occurrence",L"all");
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"pages all, every occurrence: %d replaced on page(s) %s\n",CkJsonObjectW_IntOf(jsonOut,L"replacements[0].count"),CkJsonObjectW_stringOf(jsonOut,L"replacements[0].pages"));
success = CkPdfW_WriteFile(pdf,L"qa_output/order_shipped.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Sample output:
// pages 2: found on page(s) 2
// pages all, first occurrence: found on page(s) 1
// pages all, every occurrence: 2 replaced on page(s) 1,2
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}