Tcl Requires Chilkat v11.7.0+
Tcl
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 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
# 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
set pdf [new_CkPdf]
set info [new_CkJsonObject]
# A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
set success [CkPdf_PreLoadCheck $pdf "qa_data/pdf/two_page_memo.pdf" $info]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $info
exit
}
puts "two_page_memo.pdf"
puts " encrypted: [CkJsonObject_BoolOf $info encrypted]"
# A PDF that needs a password to open.
set success [CkPdf_PreLoadCheck $pdf "qa_data/pdf/encrypted_user_password.pdf" $info]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $info
exit
}
puts "encrypted_user_password.pdf"
puts " encrypted: [CkJsonObject_BoolOf $info encrypted]"
puts " method: [CkJsonObject_stringOf $info method]"
puts " algorithm: [CkJsonObject_stringOf $info algorithm]"
puts " openPasswordRequired: [CkJsonObject_BoolOf $info openPasswordRequired]"
# A PDF that opens without a password but is restricted. The permissions are readable without
# any password.
set success [CkPdf_PreLoadCheck $pdf "qa_data/pdf/no_copy_permission.pdf" $info]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $info
exit
}
puts "no_copy_permission.pdf"
puts " encrypted: [CkJsonObject_BoolOf $info encrypted]"
puts " openPasswordRequired: [CkJsonObject_BoolOf $info openPasswordRequired]"
puts " may copy: [CkJsonObject_BoolOf $info permissions.copy]"
# The typical use: ask for a password only when one is needed, then load.
set success [CkPdf_PreLoadCheck $pdf "qa_data/pdf/encrypted_user_password.pdf" $info]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $info
exit
}
if {[CkJsonObject_BoolOf $info "openPasswordRequired"] == 1} then {
# An application would prompt for the password here.
CkPdf_put_UserPassword $pdf "secret"
}
set success [CkPdf_LoadFile $pdf "qa_data/pdf/encrypted_user_password.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $info
exit
}
puts "loaded, pages: [CkPdf_get_NumPages $pdf]"
# 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
delete_CkPdf $pdf
delete_CkJsonObject $info