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

How a PDF Watermark Is Stored: artifact, annotation, layer or content

See more PDF Watermark Examples

Demonstrates the approach member, which decides how the watermark is stored and therefore what can be done with it afterwards. artifact (the default) is marked content that viewers draw, text extraction leaves out and Chilkat can remove later. annotation is a Watermark annotation, always on top, and the only approach allowed on a signed PDF while PreserveSignatures is true. layer is an optional-content layer the viewer can hide and that can be made print-only. content draws straight into the page with no marker: visible everywhere and the tamper-resistant choice, but by design it can never be listed or removed again.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkPdfW.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

    CkJsonObjectW json;
    CkJsonObjectW jsonOut;
    CkJsonObjectW filter;

    //  The same watermark stored four different ways, each in its own copy of the document.
    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    json.UpdateString(L"type",L"text");
    json.UpdateString(L"text",L"DRAFT");
    json.UpdateString(L"id",L"draft");
    json.UpdateString(L"approach",L"artifact");
    success = pdf.Watermark(L"add",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  detect answers whether anything matching the filter is in the document.  Finding nothing is
    //  still success: "found" is simply false.
    filter.UpdateString(L"id",L"draft");
    success = pdf.Watermark(L"detect",filter,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"artifact: found = %d\n",jsonOut.BoolOf(L"found"));

    //  An annotation: always drawn on top, and the one approach a signed PDF accepts.
    CkPdfW pdf2;
    success = pdf2.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

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

    success = pdf2.Watermark(L"detect",filter,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf2.lastErrorText());
        return;
    }

    wprintf(L"annotation: found = %d\n",jsonOut.BoolOf(L"found"));

    //  A layer, which the viewer's layers panel can switch off.
    CkPdfW pdf3;
    success = pdf3.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    json.UpdateString(L"approach",L"layer");
    json.UpdateString(L"layer.name",L"Draft marking");
    success = pdf3.Watermark(L"add",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    success = pdf3.Watermark(L"detect",filter,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf3.lastErrorText());
        return;
    }

    wprintf(L"layer: found = %d\n",jsonOut.BoolOf(L"found"));

    //  Drawn into the page content with no marker.  There is nothing to find afterwards, which is the
    //  point of it: it cannot be stripped out by a tool that removes watermarks.
    CkPdfW pdf4;
    success = pdf4.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf4.lastErrorText());
        return;
    }

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

    success = pdf4.Watermark(L"detect",filter,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf4.lastErrorText());
        return;
    }

    wprintf(L"content: found = %d\n",jsonOut.BoolOf(L"found"));

    //  Sample output:

    //  artifact: found = True
    //  annotation: found = True
    //  layer: found = True
    //  content: found = False
    }