Zig Requires Chilkat v11.7.0+
Zig
Find Out Whether a PDF Needs a Password Before Loading It
See more PDF Protection Examples
Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.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
// The PDF used in this example is available at encrypted_user_password.pdf
// The PDF used in this example is available at no_copy_permission.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
const info = try chilkat.JsonObject.init();
defer info.deinit();
// A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
pdf.preLoadCheck("qa_data/pdf/two_page_memo.pdf", info) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("two_page_memo.pdf\n", .{});
std.debug.print(" encrypted: {}\n", .{info.boolOf("encrypted")});
// A PDF that needs a password to open.
pdf.preLoadCheck("qa_data/pdf/encrypted_user_password.pdf", info) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("encrypted_user_password.pdf\n", .{});
std.debug.print(" encrypted: {}\n", .{info.boolOf("encrypted")});
std.debug.print(" method: {s}\n", .{try info.stringOf(alloc, "method")});
std.debug.print(" algorithm: {s}\n", .{try info.stringOf(alloc, "algorithm")});
std.debug.print(" openPasswordRequired: {}\n", .{info.boolOf("openPasswordRequired")});
// A PDF that opens without a password but is restricted. The permissions are readable without
// any password.
pdf.preLoadCheck("qa_data/pdf/no_copy_permission.pdf", info) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("no_copy_permission.pdf\n", .{});
std.debug.print(" encrypted: {}\n", .{info.boolOf("encrypted")});
std.debug.print(" openPasswordRequired: {}\n", .{info.boolOf("openPasswordRequired")});
std.debug.print(" may copy: {}\n", .{info.boolOf("permissions.copy")});
// The typical use: ask for a password only when one is needed, then load.
pdf.preLoadCheck("qa_data/pdf/encrypted_user_password.pdf", info) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
if (info.boolOf("openPasswordRequired")) {
// An application would prompt for the password here.
pdf.setUserPassword("secret");
}
pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("loaded, pages: {d}\n", .{pdf.getNumPages()});
// Sample output:
// two_page_memo.pdf
// encrypted: False
// encrypted_user_password.pdf
// encrypted: True
// method: password
// algorithm: rc4-128
// openPasswordRequired: True
// no_copy_permission.pdf
// encrypted: True
// openPasswordRequired: False
// may copy: False
// loaded, pages: 1
}