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

Remove a Watermark from a PDF

See more PDF Watermark Examples

Demonstrates remove, which deletes the watermarks matching the filter and reports how many went. The filter is the same one list takes, so the usual sequence is to list what is there and then remove by id, by source, or by text. An empty filter is refused unless "all" is true, so that a whole document cannot be stripped by an empty JSON object. Removing finds nothing is not an error: "removed" is simply 0. An annotation is deleted, the marked content of an artifact and any form it alone used are deleted, and the content of a layer goes with the layer once nothing else uses it. A watermark added with the content approach is part of the page and is never removable.

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 acrobat_watermark.pdf

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

    const filter = try chilkat.JsonObject.init();
    defer filter.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();

    // What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
    pdf.watermark("list", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("watermarks: {d}\n", .{json_out.intOf("count")});
    json_out.setI(0);
    std.debug.print("first id: {s} text: {s}\n", .{ try json_out.stringOf(alloc, "watermarks[i].id"), try json_out.stringOf(alloc, "watermarks[i].text") });

    // Add one of our own beside it, so that the filters below have two to choose between.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("type", "text") catch {};
    json.updateString("text", "SUPERSEDED") catch {};
    json.updateString("id", "superseded") catch {};
    pdf.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Remove only what Acrobat put there, leaving ours in place.
    filter.updateString("source", "acrobat") catch {};
    pdf.watermark("remove", filter, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("removed (acrobat): {d}\n", .{json_out.intOf("removed")});

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

    std.debug.print("left in the document: {d}\n", .{json_out.intOf("count")});

    // An empty filter is refused for remove: say all explicitly to strip everything.
    pdf.watermark("remove", empty, json_out) catch {
        std.debug.print("An empty filter was refused, as it should be.\n", .{});
    };

    empty.updateBool("all", true) catch {};
    pdf.watermark("remove", empty, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("removed (all): {d}\n", .{json_out.intOf("removed")});

    // The changes are in memory until the file is written.
    pdf.writeFile("qa_output/acrobat_unwatermarked.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Sample output:

    // watermarks: 1
    // first id: ext1 text: Sample
    // removed (acrobat): 1
    // left in the document: 1
    // An empty filter was refused, as it should be.
    // removed (all): 1
}