Lua Requires Chilkat v11.7.0+
Lua
Change the Permissions of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.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 encrypted_user_password.pdf
local pdf = chilkat.Pdf.new()
-- Changing the permissions needs owner rights. Both passwords are given here, which is what
-- an AES-256 PDF requires for this operation.
pdf.user_password = "secret"
pdf.owner_password = "owner-secret"
local success = pdf:load_file("qa_data/pdf/encrypted_user_password.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- No printing and no copying. Everything not mentioned is granted.
local json = chilkat.JsonObject.new()
json:update_string("permissions.print", "none")
json:update_bool("permissions.copy", false)
local jsonOut = chilkat.JsonObject.new()
success = pdf:protection("changePermissions", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("print: " .. jsonOut:string_of("pending.permissions.print"))
print("copy: " .. tostring(jsonOut:bool_of("pending.permissions.copy")))
print("modify: " .. tostring(jsonOut:bool_of("pending.permissions.modify")))
print("password still needed to open: " .. tostring(jsonOut:bool_of("pending.userPasswordRequired")))
success = pdf:write_file("qa_output/minutes_no_print.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- The same user password opens the result, now with the new permissions.
local reader = chilkat.Pdf.new()
reader.user_password = "secret"
success = reader:load_file("qa_output/minutes_no_print.pdf")
if not success then
print(reader.last_error_text)
return
end
local empty = chilkat.JsonObject.new()
local info = chilkat.JsonObject.new()
success = reader:protection("getInfo", empty, info)
if not success then
print(reader.last_error_text)
return
end
print("saved file, print: " .. info:string_of("loaded.permissions.print"))
print("saved file, copy: " .. tostring(info:bool_of("loaded.permissions.copy")))
-- Sample output:
-- print: none
-- copy: False
-- modify: True
-- password still needed to open: True
-- saved file, print: none
-- saved file, copy: False