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

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    CkJsonObjectW json;
    json.UpdateString(L"type",L"text");
    json.UpdateString(L"text",L"UNCONTROLLED WHEN PRINTED");
    json.UpdateString(L"id",L"printonly");

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

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

    CkJsonObjectW jsonOut;
    success = pdf.Watermark(L"add",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"layer id: %s pages: %s\n",jsonOut.stringOf(L"id"),jsonOut.stringOf(L"pages"));

    success = pdf.WriteFile(L"qa_output/memo_print_only.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  The same thing as an annotation instead.
    CkPdfW pdf2;
    success = pdf2.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    CkJsonObjectW json2;
    json2.UpdateString(L"type",L"text");
    json2.UpdateString(L"text",L"UNCONTROLLED WHEN PRINTED");
    json2.UpdateString(L"id",L"printonly2");
    json2.UpdateString(L"approach",L"annotation");
    json2.UpdateBool(L"annotation.view",false);
    json2.UpdateBool(L"annotation.print",true);
    json2.UpdateBool(L"annotation.fixedPrint",true);
    json2.UpdateString(L"rotation",L"diagonal");
    success = pdf2.Watermark(L"add",json2,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    wprintf(L"annotation id: %s\n",jsonOut.stringOf(L"id"));

    //  Sample output:

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