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

A PDF Watermark That Only Appears When Printed

See more PDF Watermark Examples

Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.

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 two_page_memo.pdf

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/two_page_memo.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"UNCONTROLLED WHEN PRINTED");
    CkJsonObjectW_UpdateString(json,L"id",L"printonly");

    //  As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
    CkJsonObjectW_UpdateString(json,L"approach",L"layer");
    CkJsonObjectW_UpdateString(json,L"layer.name",L"Print marking");
    CkJsonObjectW_UpdateString(json,L"layer.view",L"off");
    CkJsonObjectW_UpdateString(json,L"layer.print",L"on");
    CkJsonObjectW_UpdateBool(json,L"layer.locked",TRUE);

    CkJsonObjectW_UpdateString(json,L"rotation",L"diagonal");
    CkJsonObjectW_UpdateString(json,L"scale.fit",L"page");
    CkJsonObjectW_UpdateNumber(json,L"scale.fitPercent",L"70");
    CkJsonObjectW_UpdateNumber(json,L"opacity",L"0.3");

    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"layer id: %s pages: %s\n",CkJsonObjectW_stringOf(jsonOut,L"id"),CkJsonObjectW_stringOf(jsonOut,L"pages"));

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

    //  The same thing as an annotation instead.
    pdf2 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf2,L"qa_data/pdf/two_page_memo.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"UNCONTROLLED WHEN PRINTED");
    CkJsonObjectW_UpdateString(json2,L"id",L"printonly2");
    CkJsonObjectW_UpdateString(json2,L"approach",L"annotation");
    CkJsonObjectW_UpdateBool(json2,L"annotation.view",FALSE);
    CkJsonObjectW_UpdateBool(json2,L"annotation.print",TRUE);
    CkJsonObjectW_UpdateBool(json2,L"annotation.fixedPrint",TRUE);
    CkJsonObjectW_UpdateString(json2,L"rotation",L"diagonal");
    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"annotation id: %s\n",CkJsonObjectW_stringOf(jsonOut,L"id"));

    //  Sample output:

    //  layer id: printonly pages: 1-2
    //  annotation id: printonly2


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

    }