Zig Requires Chilkat v11.7.0+
Zig
Find Out Whether a PDF Has a Watermark
See more PDF Watermark Examples
Demonstrates detect, which answers yes or no, and list, which enumerates what is there. Both recognize the watermarks Chilkat writes and the standard forms other tools write: Watermark annotations from any producer, marked content tagged as a Watermark or Background artifact, Acrobat watermarks and backgrounds, and optional-content layers whose name says watermark. Acrobat headers and footers are not watermarks and are ignored. A watermark from another producer has no id of its own, so Chilkat assigns one (ext1, ext2, ...) that stays valid until the PDF is changed, which is what makes a list followed by a remove by id work. Properties that cannot be read back are left out rather than guessed.Chilkat Zig Downloads
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_two_watermarks.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/acrobat_two_watermarks.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// An empty filter means every watermark in the document.
const filter = try chilkat.JsonObject.init();
defer filter.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.watermark("detect", filter, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("found: {} count: {d}\n", .{ json_out.boolOf("found"), json_out.intOf("count") });
pdf.watermark("list", filter, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
var i: i32 = 0;
const count_i = json_out.sizeOfArray("watermarks");
while (i < count_i) {
json_out.setI(i);
std.debug.print("id: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].id")});
std.debug.print(" source: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].source")});
std.debug.print(" approach: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].approach")});
std.debug.print(" type: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].type")});
std.debug.print(" text: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].text")});
std.debug.print(" pages: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].pages")});
std.debug.print(" zOrder: {s}\n", .{try json_out.stringOf(alloc, "watermarks[i].zOrder")});
std.debug.print(" removable: {}\n", .{json_out.boolOf("watermarks[i].removable")});
i = i + 1;
}
// The filter narrows what is reported: by producer (chilkat, acrobat or other), by approach, by
// pages, by an exact id or label, or by text with textContains.
const by_source = try chilkat.JsonObject.init();
defer by_source.deinit();
by_source.updateString("source", "acrobat") catch {};
pdf.watermark("detect", by_source, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("made by Acrobat: {d}\n", .{json_out.intOf("count")});
// Sample output:
// found: True count: 2
// id: ext1
// source: acrobat
// approach: artifact
// type: image
// text:
// pages: 1
// zOrder: front
// removable: True
// id: ext2
// source: acrobat
// approach: artifact
// type: text
// text: sample
// pages: 1
// zOrder: front
// removable: True
// made by Acrobat: 2
}