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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("type", "text") catch {};
    json.updateString("text", "UNCONTROLLED WHEN PRINTED") catch {};
    json.updateString("id", "printonly") catch {};

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

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

    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("layer id: {s} pages: {s}\n", .{ try json_out.stringOf(alloc, "id"), try json_out.stringOf(alloc, "pages") });

    pdf.writeFile("qa_output/memo_print_only.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // The same thing as an annotation instead.
    const pdf2 = try chilkat.Pdf.init();
    defer pdf2.deinit();
    pdf2.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    const json2 = try chilkat.JsonObject.init();
    defer json2.deinit();
    json2.updateString("type", "text") catch {};
    json2.updateString("text", "UNCONTROLLED WHEN PRINTED") catch {};
    json2.updateString("id", "printonly2") catch {};
    json2.updateString("approach", "annotation") catch {};
    json2.updateBool("annotation.view", false) catch {};
    json2.updateBool("annotation.print", true) catch {};
    json2.updateBool("annotation.fixedPrint", true) catch {};
    json2.updateString("rotation", "diagonal") catch {};
    pdf2.watermark("add", json2, json_out) catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("annotation id: {s}\n", .{try json_out.stringOf(alloc, "id")});

    // Sample output:

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