Zig Requires Chilkat v11.7.0+
Zig
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.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();
// Removing the encryption needs owner rights: load with the owner password.
pdf.setOwnerPassword("owner-secret");
pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
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, as loaded: {}\n", .{json_out.boolOf("loaded.encrypted")});
std.debug.print("algorithm: {s}\n", .{try json_out.stringOf(alloc, "loaded.algorithm")});
pdf.protection("removeEncryption", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("will be saved encrypted: {}\n", .{json_out.boolOf("pending.encrypted")});
pdf.writeFile("qa_output/minutes_unprotected.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// The result is an ordinary PDF that opens without a password.
const reader = try chilkat.Pdf.init();
defer reader.deinit();
const info = try chilkat.JsonObject.init();
defer info.deinit();
reader.preLoadCheck("qa_output/minutes_unprotected.pdf", info) catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
std.debug.print("encrypted: {}\n", .{info.boolOf("encrypted")});
reader.loadFile("qa_output/minutes_unprotected.pdf") catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
std.debug.print("pages: {d}\n", .{reader.getNumPages()});
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
}