Zig Requires Chilkat v11.7.0+
Zig
Optimizing an Encrypted PDF
See more PDF Optimization Examples
Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.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 pdf = try chilkat.Pdf.init();
defer pdf.deinit();
// The password must be set before loading.
pdf.setUserPassword("secret");
pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// What security does the file have, and may it be modified?
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.protection("getInfo", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("encrypted: {}\n", .{json_out.boolOf("loaded.encrypted")});
std.debug.print("method: {s}\n", .{try json_out.stringOf(alloc, "loaded.method")});
std.debug.print("openedWith: {s}\n", .{try json_out.stringOf(alloc, "loaded.openedWith")});
std.debug.print("may modify: {}\n", .{json_out.boolOf("loaded.permissions.modify")});
pdf.optimize("optimize", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
pdf.writeFile("qa_output/encrypted_optimized.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("bytesSaved: {d}\n", .{json_out.intOf("bytesSaved")});
// The output is still encrypted with the same security: loading it without the password fails.
const no_password = try chilkat.Pdf.init();
defer no_password.deinit();
try no_password.loadFile("qa_output/encrypted_optimized.pdf");
const q = try chilkat.JsonObject.init();
defer q.deinit();
const q_out = try chilkat.JsonObject.init();
defer q_out.deinit();
try no_password.extractText("assess", q, q_out);
std.debug.print("passwordRequired without the password: {}\n", .{q_out.boolOf("quality.passwordRequired")});
// With the password it reads normally.
const with_password = try chilkat.Pdf.init();
defer with_password.deinit();
with_password.setUserPassword("secret");
with_password.loadFile("qa_output/encrypted_optimized.pdf") catch {
std.debug.print("{s}\n", .{try with_password.getLastErrorText(alloc)});
return;
};
std.debug.print("pages: {d}\n", .{with_password.getNumPages()});
}