Lua Requires Chilkat v11.7.0+
Lua
PDF Owner Password vs. User Password
See more PDF Protection Examples
Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.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 json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
-- Opened with the user password.
local user = chilkat.Pdf.new()
user.user_password = "secret"
local success = user:load_file("qa_data/pdf/encrypted_user_password.pdf")
if not success then
print(user.last_error_text)
return
end
success = user:protection("getInfo", json, jsonOut)
if not success then
print(user.last_error_text)
return
end
print("opened with: " .. jsonOut:string_of("loaded.openedWith"))
-- Changing the security needs owner rights, so this is refused.
success = user:protection("removeEncryption", json, jsonOut)
if not success then
print("removeEncryption was refused: the PDF was not opened with its owner password.")
end
-- Opened with the owner password.
local owner = chilkat.Pdf.new()
owner.owner_password = "owner-secret"
success = owner:load_file("qa_data/pdf/encrypted_user_password.pdf")
if not success then
print(owner.last_error_text)
return
end
success = owner:protection("getInfo", json, jsonOut)
if not success then
print(owner.last_error_text)
return
end
print("opened with: " .. jsonOut:string_of("loaded.openedWith"))
-- Now it is allowed.
success = owner:protection("removeEncryption", json, jsonOut)
if not success then
print(owner.last_error_text)
return
end
print("removeEncryption succeeded; will be saved encrypted: " .. tostring(jsonOut:bool_of("pending.encrypted")))
-- Sample output:
-- opened with: user
-- removeEncryption was refused: the PDF was not opened with its owner password.
-- opened with: owner
-- removeEncryption succeeded; will be saved encrypted: False