Tcl Requires Chilkat v11.7.0+
Tcl
Password-Protect a PDF with AES-256 Encryption
See more PDF Protection Examples
Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.Chilkat Tcl Downloads
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 two_page_memo.pdf
set pdf [new_CkPdf]
set success [CkPdf_LoadFile $pdf "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
exit
}
# The passwords are taken from these two properties at the time Protection is called.
# The user password opens the PDF. The owner password opens it too, and is the one needed
# later to change or remove the protection.
CkPdf_put_UserPassword $pdf "user-secret"
CkPdf_put_OwnerPassword $pdf "owner-secret"
# An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
set json [new_CkJsonObject]
set jsonOut [new_CkJsonObject]
set success [CkPdf_Protection $pdf "encrypt" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
puts "algorithm: [CkJsonObject_stringOf $jsonOut pending.algorithm]"
puts "PDF version: [CkJsonObject_stringOf $jsonOut pending.pdfVersion]"
puts "password needed to open: [CkJsonObject_BoolOf $jsonOut pending.userPasswordRequired]"
# Writing the file applies it.
set success [CkPdf_WriteFile $pdf "qa_output/memo_encrypted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# PreLoadCheck reports a file's encryption without loading it, and needs no password.
set checker [new_CkPdf]
set info [new_CkJsonObject]
set success [CkPdf_PreLoadCheck $checker "qa_output/memo_encrypted.pdf" $info]
if {$success == 0} then {
puts [CkPdf_lastErrorText $checker]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $checker
delete_CkJsonObject $info
exit
}
puts "encrypted: [CkJsonObject_BoolOf $info encrypted]"
puts "openPasswordRequired: [CkJsonObject_BoolOf $info openPasswordRequired]"
# With the password, the PDF opens normally.
set reader [new_CkPdf]
CkPdf_put_UserPassword $reader "user-secret"
set success [CkPdf_LoadFile $reader "qa_output/memo_encrypted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $reader]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $checker
delete_CkJsonObject $info
delete_CkPdf $reader
exit
}
puts "pages: [CkPdf_get_NumPages $reader]"
# Sample output:
# algorithm: aes-256
# PDF version: 1.7
# password needed to open: True
# encrypted: True
# openPasswordRequired: True
# pages: 2
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $checker
delete_CkJsonObject $info
delete_CkPdf $reader