Sample code for 30+ languages & platforms
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 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 two_page_memo.pdf

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/two_page_memo.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","UNCONTROLLED WHEN PRINTED");
    CkJsonObject_UpdateString(json,"id","printonly");

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

    CkJsonObject_UpdateString(json,"rotation","diagonal");
    CkJsonObject_UpdateString(json,"scale.fit","page");
    CkJsonObject_UpdateNumber(json,"scale.fitPercent","70");
    CkJsonObject_UpdateNumber(json,"opacity","0.3");

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

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

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

    //  Sample output:

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


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

    }