Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.7.0+

Remove the Password and Encryption from a PDF

See more PDF Protection Examples

Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.

Chilkat Lua Downloads

Lua
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()

-- Removing the encryption needs owner rights: load with the owner password.
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

local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
success = pdf:protection("getInfo", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("encrypted, as loaded: " .. tostring(jsonOut:bool_of("loaded.encrypted")))
print("algorithm: " .. jsonOut:string_of("loaded.algorithm"))

success = pdf:protection("removeEncryption", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("will be saved encrypted: " .. tostring(jsonOut:bool_of("pending.encrypted")))

success = pdf:write_file("qa_output/minutes_unprotected.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

-- The result is an ordinary PDF that opens without a password.
local reader = chilkat.Pdf.new()
local info = chilkat.JsonObject.new()
success = reader:pre_load_check("qa_output/minutes_unprotected.pdf", info)
if not success then
    print(reader.last_error_text)
    return
end

print("encrypted: " .. tostring(info:bool_of("encrypted")))
success = reader:load_file("qa_output/minutes_unprotected.pdf")
if not success then
    print(reader.last_error_text)
    return
end

print("pages: " .. reader.num_pages)

-- Sample output:

-- encrypted, as loaded: True
-- algorithm: rc4-128
-- will be saved encrypted: False
-- encrypted: False
-- pages: 1