Zig Requires Chilkat v11.7.0+
Zig
Encrypt a PDF for a Certificate Instead of a Password
See more PDF Protection Examples
Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.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 recipient's certificate. Only the public certificate is needed to encrypt.
const recipient = try chilkat.Cert.init();
defer recipient.deinit();
recipient.loadFromFile("qa_data/certs/recipient.cer") catch {
std.debug.print("{s}\n", .{try recipient.getLastErrorText(alloc)});
return;
};
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;
};
// Call AddEncryptCert once for each recipient.
pdf.addEncryptCert(recipient) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("method", "certificate") catch {};
json.updateBool("recipientPermissions[0].changeSecurity", true) catch {};
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;
};
std.debug.print("method: {s}\n", .{try json_out.stringOf(alloc, "pending.method")});
std.debug.print("algorithm: {s}\n", .{try json_out.stringOf(alloc, "pending.algorithm")});
std.debug.print("recipients: {d}\n", .{json_out.sizeOfArray("pending.recipients")});
pdf.writeFile("qa_output/memo_for_recipient.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// PreLoadCheck says how the file is protected, without loading it.
const reader = try chilkat.Pdf.init();
defer reader.deinit();
const info = try chilkat.JsonObject.init();
defer info.deinit();
reader.preLoadCheck("qa_output/memo_for_recipient.pdf", info) catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
std.debug.print("saved file, method: {s}\n", .{try info.stringOf(alloc, "method")});
std.debug.print("saved file, recipients: {d}\n", .{info.sizeOfArray("recipients")});
// The recipient opens it with the certificate and its private key, here from a .pfx file.
// (On Windows, a certificate that is installed in the Windows certificate store with its private
// key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
const my_cert = try chilkat.Cert.init();
defer my_cert.deinit();
my_cert.loadPfxFile("qa_data/pfx/recipient.pfx", "pfx-password") catch {
std.debug.print("{s}\n", .{try my_cert.getLastErrorText(alloc)});
return;
};
reader.setDecryptCert(my_cert) catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
reader.loadFile("qa_output/memo_for_recipient.pdf") catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
const empty = try chilkat.JsonObject.init();
defer empty.deinit();
const loaded_info = try chilkat.JsonObject.init();
defer loaded_info.deinit();
reader.protection("getInfo", empty, loaded_info) catch {
std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
return;
};
std.debug.print("opened with: {s}\n", .{try loaded_info.stringOf(alloc, "loaded.openedWith")});
std.debug.print("pages: {d}\n", .{reader.getNumPages()});
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
}