AutoIt Requires Chilkat v11.7.0+
AutoIt
Change the Passwords of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.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")
; Load with the current passwords.
$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
; The new passwords. Protection reads the properties as they are when it is called.
$oPdf.UserPassword = "new-user-secret"
$oPdf.OwnerPassword = "new-owner-secret"
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Protection("changePasswords",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; The algorithm and the permissions are kept.
ConsoleWrite("algorithm: " & $oJsonOut.StringOf("pending.algorithm") & @CRLF)
ConsoleWrite("password needed to open: " & $oJsonOut.BoolOf("pending.userPasswordRequired") & @CRLF)
$bSuccess = $oPdf.WriteFile("qa_output/minutes_new_passwords.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; The new user password opens the result.
$oReader = ObjCreate("Chilkat.Pdf")
$oReader.UserPassword = "new-user-secret"
$bSuccess = $oReader.LoadFile("qa_output/minutes_new_passwords.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oReader.Protection("getInfo",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("with the new user password, opened with: " & $oJsonOut.StringOf("loaded.openedWith") & @CRLF)
; And so does the new owner password.
$oOwner = ObjCreate("Chilkat.Pdf")
$oOwner.OwnerPassword = "new-owner-secret"
$bSuccess = $oOwner.LoadFile("qa_output/minutes_new_passwords.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oOwner.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oOwner.Protection("getInfo",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oOwner.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("with the new owner password, opened with: " & $oJsonOut.StringOf("loaded.openedWith") & @CRLF)
; Sample output:
; algorithm: rc4-128
; password needed to open: True
; with the new user password, opened with: user
; with the new owner password, opened with: owner