Sample code for 30+ languages & platforms
Java 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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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 pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        json.updateString("type", "text");
        json.updateString("text", "UNCONTROLLED WHEN PRINTED");
        json.updateString("id", "printonly");

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

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

        JsonObject jsonOut = new JsonObject();
        try {
            pdf.watermark("add", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("layer id: " + jsonOut.stringOf("id") + " pages: " + jsonOut.stringOf("pages"));

        try {
            pdf.writeFile("qa_output/memo_print_only.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The same thing as an annotation instead.
        Pdf pdf2 = new Pdf();
        try {
            pdf2.loadFile("qa_data/pdf/two_page_memo.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json2 = new JsonObject();
        json2.updateString("type", "text");
        json2.updateString("text", "UNCONTROLLED WHEN PRINTED");
        json2.updateString("id", "printonly2");
        json2.updateString("approach", "annotation");
        json2.updateBool("annotation.view", false);
        json2.updateBool("annotation.print", true);
        json2.updateBool("annotation.fixedPrint", true);
        json2.updateString("rotation", "diagonal");
        try {
            pdf2.watermark("add", json2, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("annotation id: " + jsonOut.stringOf("id"));

        // Sample output:

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