Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkPdf pdf2;
    HCkJsonObject 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 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/four_pages.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        return;
    }

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

    jsonOut = CkJsonObject_Create();
    success = CkPdf_Watermark(pdf,"add",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("footer pages: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));

    //  Roman numerals that start at i on page 2, so that a cover page is not numbered.
    pdf2 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf2,"qa_data/pdf/four_pages.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf2);
        return;
    }

    json2 = CkJsonObject_Create();
    CkJsonObject_UpdateString(json2,"type","text");
    CkJsonObject_UpdateString(json2,"text","- {page} -");
    CkJsonObject_UpdateString(json2,"id","roman");
    CkJsonObject_UpdateString(json2,"pages","2-");
    CkJsonObject_UpdateInt(json2,"pageNumber.start",1);
    CkJsonObject_UpdateString(json2,"pageNumber.style","romanLower");
    CkJsonObject_UpdateString(json2,"position.align","bottom");
    CkJsonObject_UpdateNumber(json2,"font.size","10");
    success = CkPdf_Watermark(pdf2,"add",json2,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf2);
        CkJsonObject_Dispose(json2);
        return;
    }

    printf("roman pages: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));

    success = CkPdf_WriteFile(pdf,"qa_output/four_pages_numbered.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf2);
        CkJsonObject_Dispose(json2);
        return;
    }

    //  Sample output:

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


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkPdf_Dispose(pdf2);
    CkJsonObject_Dispose(json2);

    }