Zig Requires Chilkat v11.7.0+
Zig
Watermark Only Some of a PDF's Pages
See more PDF Watermark Examples
Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.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 four_pages.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/four_pages.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("pages in the document: {d}\n", .{pdf.getNumPages()});
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
json.updateString("type", "text") catch {};
json.updateString("text", "INTERNAL") catch {};
json.updateString("id", "internal") catch {};
// Every page except the first.
json.updateString("pages", "all") catch {};
json.updateString("excludePages", "1") catch {};
pdf.watermark("add", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("every page but the cover: {s}\n", .{try json_out.stringOf(alloc, "pages")});
// A list of pages and ranges, on a second copy of the document.
const pdf2 = try chilkat.Pdf.init();
defer pdf2.deinit();
pdf2.loadFile("qa_data/pdf/four_pages.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", "APPENDIX") catch {};
json2.updateString("id", "appendix") catch {};
json2.updateString("pages", "2,4-") catch {};
pdf2.watermark("add", json2, json_out) catch {
std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
return;
};
std.debug.print("page 2 and 4 onwards: {s}\n", .{try json_out.stringOf(alloc, "pages")});
pdf.writeFile("qa_output/four_pages_internal.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4
}