Lua Requires Chilkat v11.7.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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.
local recipient = chilkat.Cert.new()
local success = recipient:load_from_file("qa_data/certs/recipient.cer")
if not success then
print(recipient.last_error_text)
return
end
local pdf = chilkat.Pdf.new()
success = pdf:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- Call AddEncryptCert once for each recipient.
success = pdf:add_encrypt_cert(recipient)
if not success then
print(pdf.last_error_text)
return
end
-- recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
-- recipient owner rights, so the file can be managed later.
local json = chilkat.JsonObject.new()
json:update_string("method", "certificate")
json:update_bool("recipientPermissions[0].changeSecurity", true)
local jsonOut = chilkat.JsonObject.new()
success = pdf:protection("encrypt", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("method: " .. jsonOut:string_of("pending.method"))
print("algorithm: " .. jsonOut:string_of("pending.algorithm"))
print("recipients: " .. jsonOut:size_of_array("pending.recipients"))
success = pdf:write_file("qa_output/memo_for_recipient.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- PreLoadCheck says how the file is protected, without loading it.
local reader = chilkat.Pdf.new()
local info = chilkat.JsonObject.new()
success = reader:pre_load_check("qa_output/memo_for_recipient.pdf", info)
if not success then
print(reader.last_error_text)
return
end
print("saved file, method: " .. info:string_of("method"))
print("saved file, recipients: " .. info:size_of_array("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.)
local myCert = chilkat.Cert.new()
success = myCert:load_pfx_file("qa_data/pfx/recipient.pfx", "pfx-password")
if not success then
print(myCert.last_error_text)
return
end
success = reader:set_decrypt_cert(myCert)
if not success then
print(reader.last_error_text)
return
end
success = reader:load_file("qa_output/memo_for_recipient.pdf")
if not success then
print(reader.last_error_text)
return
end
local empty = chilkat.JsonObject.new()
local loadedInfo = chilkat.JsonObject.new()
success = reader:protection("getInfo", empty, loadedInfo)
if not success then
print(reader.last_error_text)
return
end
print("opened with: " .. loadedInfo:string_of("loaded.openedWith"))
print("pages: " .. reader.num_pages)
-- Sample output:
-- method: certificate
-- algorithm: aes-256
-- recipients: 1
-- saved file, method: certificate
-- saved file, recipients: 1
-- opened with: certificate
-- pages: 2