AutoIt Requires Chilkat v11.7.0+
AutoIt
Change the Permissions of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.Chilkat AutoIt Downloads
Local $bSuccess = False
; 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
$oPdf = ObjCreate("Chilkat.Pdf")
; Changing the permissions needs owner rights. Both passwords are given here, which is what
; an AES-256 PDF requires for this operation.
$oPdf.UserPassword = "secret"
$oPdf.OwnerPassword = "owner-secret"
$bSuccess = $oPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; No printing and no copying. Everything not mentioned is granted.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("permissions.print","none")
$oJson.UpdateBool("permissions.copy",False)
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Protection("changePermissions",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("print: " & $oJsonOut.StringOf("pending.permissions.print") & @CRLF)
ConsoleWrite("copy: " & $oJsonOut.BoolOf("pending.permissions.copy") & @CRLF)
ConsoleWrite("modify: " & $oJsonOut.BoolOf("pending.permissions.modify") & @CRLF)
ConsoleWrite("password still needed to open: " & $oJsonOut.BoolOf("pending.userPasswordRequired") & @CRLF)
$bSuccess = $oPdf.WriteFile("qa_output/minutes_no_print.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; The same user password opens the result, now with the new permissions.
$oReader = ObjCreate("Chilkat.Pdf")
$oReader.UserPassword = "secret"
$bSuccess = $oReader.LoadFile("qa_output/minutes_no_print.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
$oEmpty = ObjCreate("Chilkat.JsonObject")
$oInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReader.Protection("getInfo",$oEmpty,$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("saved file, print: " & $oInfo.StringOf("loaded.permissions.print") & @CRLF)
ConsoleWrite("saved file, copy: " & $oInfo.BoolOf("loaded.permissions.copy") & @CRLF)
; Sample output:
; print: none
; copy: False
; modify: True
; password still needed to open: True
; saved file, print: none
; saved file, copy: False