Zig Requires Chilkat v11.7.0+
Zig
Page Numbers, Dates and the File Name in a PDF Watermark
See more PDF Watermark Examples
Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.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;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("type", "text") catch {};
json.updateString("text", "Page {page} of {pages} - printed {date:yyyy-MM-dd HH:mm} - {filename}") catch {};
json.updateString("id", "pagefoot") catch {};
json.updateString("position.align", "bottom") catch {};
json.updateNumber("position.margin", "24") catch {};
json.updateNumber("font.size", "9") catch {};
json.updateString("color", "#505050") 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("footer pages: {s}\n", .{try json_out.stringOf(alloc, "pages")});
// Roman numerals that start at i on page 2, so that a cover page is not numbered.
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", "- {page} -") catch {};
json2.updateString("id", "roman") catch {};
json2.updateString("pages", "2-") catch {};
json2.updateInt("pageNumber.start", 1) catch {};
json2.updateString("pageNumber.style", "romanLower") catch {};
json2.updateString("position.align", "bottom") catch {};
json2.updateNumber("font.size", "10") catch {};
pdf2.watermark("add", json2, json_out) catch {
std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
return;
};
std.debug.print("roman pages: {s}\n", .{try json_out.stringOf(alloc, "pages")});
pdf.writeFile("qa_output/four_pages_numbered.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// Sample output:
// footer pages: 1-4
// roman pages: 2-4
}