Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  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

set pdf [new_CkPdf]

#  Removing the encryption needs owner rights: load with the owner password.
CkPdf_put_OwnerPassword $pdf "owner-secret"
set success [CkPdf_LoadFile $pdf "qa_data/pdf/encrypted_user_password.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

set success [CkPdf_Protection $pdf "getInfo" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "encrypted, as loaded: [CkJsonObject_BoolOf $jsonOut loaded.encrypted]"
puts "algorithm: [CkJsonObject_stringOf $jsonOut loaded.algorithm]"

set success [CkPdf_Protection $pdf "removeEncryption" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

puts "will be saved encrypted: [CkJsonObject_BoolOf $jsonOut pending.encrypted]"

set success [CkPdf_WriteFile $pdf "qa_output/minutes_unprotected.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  The result is an ordinary PDF that opens without a password.
set reader [new_CkPdf]

set info [new_CkJsonObject]

set success [CkPdf_PreLoadCheck $reader "qa_output/minutes_unprotected.pdf" $info]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $reader]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    delete_CkJsonObject $info
    exit
}

puts "encrypted: [CkJsonObject_BoolOf $info encrypted]"
set success [CkPdf_LoadFile $reader "qa_output/minutes_unprotected.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $reader]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    delete_CkJsonObject $info
    exit
}

puts "pages: [CkPdf_get_NumPages $reader]"

#  Sample output:

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

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $info