Tcl Requires Chilkat v11.7.0+
Tcl
Remove the Restrictions from a PDF
See more PDF Protection Examples
Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.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 no_copy_permission.pdf
set pdf [new_CkPdf]
# This PDF opens without a password, but copying text from it is not permitted.
# Removing the restrictions needs owner rights: load with the owner password.
CkPdf_put_OwnerPassword $pdf "owner-secret"
set success [CkPdf_LoadFile $pdf "qa_data/pdf/no_copy_permission.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 "may copy, before: [CkJsonObject_BoolOf $jsonOut loaded.permissions.copy]"
puts "password needed to open: [CkJsonObject_BoolOf $jsonOut loaded.userPasswordRequired]"
set success [CkPdf_Protection $pdf "removeRestrictions" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# With no user password and nothing restricted there is nothing left to protect, so the PDF
# will be saved unencrypted. A PDF that needs a password to open would stay encrypted and
# keep its password.
puts "will be saved encrypted: [CkJsonObject_BoolOf $jsonOut pending.encrypted]"
set success [CkPdf_WriteFile $pdf "qa_output/leaflet_unrestricted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# Anyone can now extract the text.
set reader [new_CkPdf]
set success [CkPdf_LoadFile $reader "qa_output/leaflet_unrestricted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $reader]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
exit
}
set opts [new_CkJsonObject]
set extractOut [new_CkJsonObject]
set sb [new_CkStringBuilder]
set success [CkPdf_ExtractTextSb $reader "text" $opts $sb $extractOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $reader]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb
exit
}
puts "characters extracted: [CkStringBuilder_get_Length $sb]"
# Sample output:
# may copy, before: False
# password needed to open: False
# will be saved encrypted: False
# characters extracted: 216
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb