Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.7.0+

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

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    HCkPdfW pdf2;
    HCkJsonObjectW json2;

    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

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/four_pages.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"type",L"text");
    CkJsonObjectW_UpdateString(json,L"text",L"Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}");
    CkJsonObjectW_UpdateString(json,L"id",L"pagefoot");
    CkJsonObjectW_UpdateString(json,L"position.align",L"bottom");
    CkJsonObjectW_UpdateNumber(json,L"position.margin",L"24");
    CkJsonObjectW_UpdateNumber(json,L"font.size",L"9");
    CkJsonObjectW_UpdateString(json,L"color",L"#505050");

    jsonOut = CkJsonObjectW_Create();
    success = CkPdfW_Watermark(pdf,L"add",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"footer pages: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pages"));

    //  Roman numerals that start at i on page 2, so that a cover page is not numbered.
    pdf2 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf2,L"qa_data/pdf/four_pages.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkPdfW_Dispose(pdf2);
        return;
    }

    json2 = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json2,L"type",L"text");
    CkJsonObjectW_UpdateString(json2,L"text",L"- {page} -");
    CkJsonObjectW_UpdateString(json2,L"id",L"roman");
    CkJsonObjectW_UpdateString(json2,L"pages",L"2-");
    CkJsonObjectW_UpdateInt(json2,L"pageNumber.start",1);
    CkJsonObjectW_UpdateString(json2,L"pageNumber.style",L"romanLower");
    CkJsonObjectW_UpdateString(json2,L"position.align",L"bottom");
    CkJsonObjectW_UpdateNumber(json2,L"font.size",L"10");
    success = CkPdfW_Watermark(pdf2,L"add",json2,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkPdfW_Dispose(pdf2);
        CkJsonObjectW_Dispose(json2);
        return;
    }

    wprintf(L"roman pages: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pages"));

    success = CkPdfW_WriteFile(pdf,L"qa_output/four_pages_numbered.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkPdfW_Dispose(pdf2);
        CkJsonObjectW_Dispose(json2);
        return;
    }

    //  Sample output:

    //  footer pages: 1-4
    //  roman pages: 2-4


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);
    CkPdfW_Dispose(pdf2);
    CkJsonObjectW_Dispose(json2);

    }