Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
Page Numbers, Dates and the File Name in a PDF Watermark
See more PDF Watermark Examples
Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.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 four_pages.pdf
CkPdfW pdf;
success = pdf.LoadFile(L"qa_data/pdf/four_pages.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
CkJsonObjectW json;
json.UpdateString(L"type",L"text");
json.UpdateString(L"text",L"Page {page} of {pages} - printed {date:yyyy-MM-dd HH:mm} - {filename}");
json.UpdateString(L"id",L"pagefoot");
json.UpdateString(L"position.align",L"bottom");
json.UpdateNumber(L"position.margin",L"24");
json.UpdateNumber(L"font.size",L"9");
json.UpdateString(L"color",L"#505050");
CkJsonObjectW jsonOut;
success = pdf.Watermark(L"add",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
wprintf(L"footer pages: %s\n",jsonOut.stringOf(L"pages"));
// Roman numerals that start at i on page 2, so that a cover page is not numbered.
CkPdfW pdf2;
success = pdf2.LoadFile(L"qa_data/pdf/four_pages.pdf");
if (success == false) {
wprintf(L"%s\n",pdf2.lastErrorText());
return;
}
CkJsonObjectW json2;
json2.UpdateString(L"type",L"text");
json2.UpdateString(L"text",L"- {page} -");
json2.UpdateString(L"id",L"roman");
json2.UpdateString(L"pages",L"2-");
json2.UpdateInt(L"pageNumber.start",1);
json2.UpdateString(L"pageNumber.style",L"romanLower");
json2.UpdateString(L"position.align",L"bottom");
json2.UpdateNumber(L"font.size",L"10");
success = pdf2.Watermark(L"add",json2,jsonOut);
if (success == false) {
wprintf(L"%s\n",pdf2.lastErrorText());
return;
}
wprintf(L"roman pages: %s\n",jsonOut.stringOf(L"pages"));
success = pdf.WriteFile(L"qa_output/four_pages_numbered.pdf");
if (success == false) {
wprintf(L"%s\n",pdf.lastErrorText());
return;
}
// Sample output:
// footer pages: 1-4
// roman pages: 2-4
}