Sample code for 30+ languages & platforms
Zig 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 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 json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    const filter = try chilkat.JsonObject.init();
    defer filter.deinit();

    // The same watermark stored four different ways, each in its own copy of the document.
    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;
    };

    json.updateString("type", "text") catch {};
    json.updateString("text", "DRAFT") catch {};
    json.updateString("id", "draft") catch {};
    json.updateString("approach", "artifact") catch {};
    pdf.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // detect answers whether anything matching the filter is in the document.  Finding nothing is
    // still success: "found" is simply false.
    filter.updateString("id", "draft") catch {};
    pdf.watermark("detect", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("artifact: found = {}\n", .{json_out.boolOf("found")});

    // An annotation: always drawn on top, and the one approach a signed PDF accepts.
    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;
    };

    json.updateString("approach", "annotation") catch {};
    pdf2.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    pdf2.watermark("detect", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("annotation: found = {}\n", .{json_out.boolOf("found")});

    // A layer, which the viewer's layers panel can switch off.
    const pdf3 = try chilkat.Pdf.init();
    defer pdf3.deinit();
    pdf3.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    json.updateString("approach", "layer") catch {};
    json.updateString("layer.name", "Draft marking") catch {};
    pdf3.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    pdf3.watermark("detect", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf3.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("layer: found = {}\n", .{json_out.boolOf("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.
    const pdf4 = try chilkat.Pdf.init();
    defer pdf4.deinit();
    pdf4.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf4.getLastErrorText(alloc)});
        return;
    };

    json.updateString("approach", "content") catch {};
    pdf4.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf4.getLastErrorText(alloc)});
        return;
    };

    pdf4.watermark("detect", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf4.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("content: found = {}\n", .{json_out.boolOf("found")});

    // Sample output:

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