Zig Requires Chilkat v11.7.0+
Zig
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.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 two_page_memo.pdf
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;
};
// No user password: the PDF opens without a prompt. The owner password is what protects the
// restrictions, so it must not be empty when anything is restricted.
pdf.setUserPassword("");
pdf.setOwnerPassword("owner-secret");
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("permissions.print", "lowQuality") catch {};
json.updateBool("permissions.copy", false) catch {};
json.updateBool("permissions.modify", false) catch {};
json.updateBool("permissions.annotate", false) catch {};
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.protection("encrypt", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
pdf.writeFile("qa_output/memo_restricted.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// Anyone can open the result, with the rights of a user.
const reader = try chilkat.Pdf.init();
defer reader.deinit();
reader.loadFile("qa_output/memo_restricted.pdf") catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
const empty = try chilkat.JsonObject.init();
defer empty.deinit();
const info = try chilkat.JsonObject.init();
defer info.deinit();
reader.protection("getInfo", empty, info) catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
std.debug.print("opened with: {s}\n", .{try info.stringOf(alloc, "loaded.openedWith")});
std.debug.print("print: {s}\n", .{try info.stringOf(alloc, "loaded.permissions.print")});
std.debug.print("copy: {}\n", .{info.boolOf("loaded.permissions.copy")});
std.debug.print("modify: {}\n", .{info.boolOf("loaded.permissions.modify")});
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
const opts = try chilkat.JsonObject.init();
defer opts.deinit();
const extract_out = try chilkat.JsonObject.init();
defer extract_out.deinit();
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
reader.extractTextSb("text", opts, sb, extract_out) catch {
std.debug.print("Text extraction was refused: the PDF does not permit copying.\n", .{});
};
// The owner password lifts every restriction.
const owner = try chilkat.Pdf.init();
defer owner.deinit();
owner.setOwnerPassword("owner-secret");
owner.loadFile("qa_output/memo_restricted.pdf") catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
const owner_info = try chilkat.JsonObject.init();
defer owner_info.deinit();
owner.protection("getInfo", empty, owner_info) catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
std.debug.print("opened with: {s}\n", .{try owner_info.stringOf(alloc, "loaded.openedWith")});
owner.extractTextSb("text", opts, sb, extract_out) catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
std.debug.print("characters extracted with the owner password: {d}\n", .{sb.getLength()});
// Sample output:
// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048
}