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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

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;
    }

    printf("pages in the document: %d\n",CkPdf_getNumPages(pdf));

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"type","text");
    CkJsonObject_UpdateString(json,"text","INTERNAL");
    CkJsonObject_UpdateString(json,"id","internal");

    //  Every page except the first.
    CkJsonObject_UpdateString(json,"pages","all");
    CkJsonObject_UpdateString(json,"excludePages","1");
    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("every page but the cover: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));

    //  A list of pages and ranges, on a second copy of the document.
    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","APPENDIX");
    CkJsonObject_UpdateString(json2,"id","appendix");
    CkJsonObject_UpdateString(json2,"pages","2,4-");
    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("page 2 and 4 onwards: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));

    success = CkPdf_WriteFile(pdf,"qa_output/four_pages_internal.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:

    //  pages in the document: 4
    //  every page but the cover: 2-4
    //  page 2 and 4 onwards: 2,4


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

    }