Lua Requires Chilkat v11.7.0+
Lua
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.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
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- No user password: the PDF opens without a prompt. The owner password is what protects the
-- restrictions, so it must not be empty when anything is restricted.
pdf.user_password = ""
pdf.owner_password = "owner-secret"
local json = chilkat.JsonObject.new()
json:update_string("permissions.print", "lowQuality")
json:update_bool("permissions.copy", false)
json:update_bool("permissions.modify", false)
json:update_bool("permissions.annotate", false)
-- The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
local jsonOut = chilkat.JsonObject.new()
success = pdf:protection("encrypt", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
success = pdf:write_file("qa_output/memo_restricted.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- Anyone can open the result, with the rights of a user.
local reader = chilkat.Pdf.new()
success = reader:load_file("qa_output/memo_restricted.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("opened with: " .. info:string_of("loaded.openedWith"))
print("print: " .. info:string_of("loaded.permissions.print"))
print("copy: " .. tostring(info:bool_of("loaded.permissions.copy")))
print("modify: " .. tostring(info:bool_of("loaded.permissions.modify")))
-- Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
local opts = chilkat.JsonObject.new()
local extractOut = chilkat.JsonObject.new()
local sb = chilkat.StringBuilder.new()
success = reader:extract_text_sb("text", opts, sb, extractOut)
if not success then
print("Text extraction was refused: the PDF does not permit copying.")
end
-- The owner password lifts every restriction.
local owner = chilkat.Pdf.new()
owner.owner_password = "owner-secret"
success = owner:load_file("qa_output/memo_restricted.pdf")
if not success then
print(owner.last_error_text)
return
end
local ownerInfo = chilkat.JsonObject.new()
success = owner:protection("getInfo", empty, ownerInfo)
if not success then
print(owner.last_error_text)
return
end
print("opened with: " .. ownerInfo:string_of("loaded.openedWith"))
success = owner:extract_text_sb("text", opts, sb, extractOut)
if not success then
print(owner.last_error_text)
return
end
print("characters extracted with the owner password: " .. sb.length)
-- Sample output:
-- opened with: emptyPassword
-- print: lowQuality
-- copy: False
-- modify: False
-- Text extraction was refused: the PDF does not permit copying.
-- opened with: owner
-- characters extracted with the owner password: 1048