Lua Requires Chilkat v11.7.0+
Lua
Find Out Whether a PDF Needs a Password Before Loading It
See more PDF Protection Examples
Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.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 PDF used in this example is available at encrypted_user_password.pdf
-- The PDF used in this example is available at no_copy_permission.pdf
local pdf = chilkat.Pdf.new()
local info = chilkat.JsonObject.new()
-- A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
local success = pdf:pre_load_check("qa_data/pdf/two_page_memo.pdf", info)
if not success then
print(pdf.last_error_text)
return
end
print("two_page_memo.pdf")
print(" encrypted: " .. tostring(info:bool_of("encrypted")))
-- A PDF that needs a password to open.
success = pdf:pre_load_check("qa_data/pdf/encrypted_user_password.pdf", info)
if not success then
print(pdf.last_error_text)
return
end
print("encrypted_user_password.pdf")
print(" encrypted: " .. tostring(info:bool_of("encrypted")))
print(" method: " .. info:string_of("method"))
print(" algorithm: " .. info:string_of("algorithm"))
print(" openPasswordRequired: " .. tostring(info:bool_of("openPasswordRequired")))
-- A PDF that opens without a password but is restricted. The permissions are readable without
-- any password.
success = pdf:pre_load_check("qa_data/pdf/no_copy_permission.pdf", info)
if not success then
print(pdf.last_error_text)
return
end
print("no_copy_permission.pdf")
print(" encrypted: " .. tostring(info:bool_of("encrypted")))
print(" openPasswordRequired: " .. tostring(info:bool_of("openPasswordRequired")))
print(" may copy: " .. tostring(info:bool_of("permissions.copy")))
-- The typical use: ask for a password only when one is needed, then load.
success = pdf:pre_load_check("qa_data/pdf/encrypted_user_password.pdf", info)
if not success then
print(pdf.last_error_text)
return
end
if info:bool_of("openPasswordRequired") then
-- An application would prompt for the password here.
pdf.user_password = "secret"
end
success = pdf:load_file("qa_data/pdf/encrypted_user_password.pdf")
if not success then
print(pdf.last_error_text)
return
end
print("loaded, pages: " .. pdf.num_pages)
-- Sample output:
-- two_page_memo.pdf
-- encrypted: False
-- encrypted_user_password.pdf
-- encrypted: True
-- method: password
-- algorithm: rc4-128
-- openPasswordRequired: True
-- no_copy_permission.pdf
-- encrypted: True
-- openPasswordRequired: False
-- may copy: False
-- loaded, pages: 1