Zig Requires Chilkat v11.7.0+
Zig
PDF Owner Password vs. User Password
See more PDF Protection Examples
Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.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 encrypted_user_password.pdf
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
// Opened with the user password.
const user = try chilkat.Pdf.init();
defer user.deinit();
user.setUserPassword("secret");
user.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
std.debug.print("{s}\n", .{try user.getLastErrorText(alloc)});
return;
};
user.protection("getInfo", json, json_out) catch {
std.debug.print("{s}\n", .{try user.getLastErrorText(alloc)});
return;
};
std.debug.print("opened with: {s}\n", .{try json_out.stringOf(alloc, "loaded.openedWith")});
// Changing the security needs owner rights, so this is refused.
user.protection("removeEncryption", json, json_out) catch {
std.debug.print("removeEncryption was refused: the PDF was not opened with its owner password.\n", .{});
};
// Opened with the owner password.
const owner = try chilkat.Pdf.init();
defer owner.deinit();
owner.setOwnerPassword("owner-secret");
owner.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
owner.protection("getInfo", json, json_out) catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
std.debug.print("opened with: {s}\n", .{try json_out.stringOf(alloc, "loaded.openedWith")});
// Now it is allowed.
owner.protection("removeEncryption", json, json_out) catch {
std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
return;
};
std.debug.print("removeEncryption succeeded; will be saved encrypted: {}\n", .{json_out.boolOf("pending.encrypted")});
// Sample output:
// opened with: user
// removeEncryption was refused: the PDF was not opened with its owner password.
// opened with: owner
// removeEncryption succeeded; will be saved encrypted: False
}