Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.7.0+

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

Chilkat AutoIt Downloads

AutoIt
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

$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")

;  Opened with the user password.
$oUser = ObjCreate("Chilkat.Pdf")
$oUser.UserPassword = "secret"
$bSuccess = $oUser.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oUser.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oUser.Protection("getInfo",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oUser.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("opened with: " & $oJsonOut.StringOf("loaded.openedWith") & @CRLF)

;  Changing the security needs owner rights, so this is refused.
$bSuccess = $oUser.Protection("removeEncryption",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite("removeEncryption was refused: the PDF was not opened with its owner password." & @CRLF)
EndIf

;  Opened with the owner password.
$oOwner = ObjCreate("Chilkat.Pdf")
$oOwner.OwnerPassword = "owner-secret"
$bSuccess = $oOwner.LoadFile("qa_data/pdf/encrypted_user_password.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("opened with: " & $oJsonOut.StringOf("loaded.openedWith") & @CRLF)

;  Now it is allowed.
$bSuccess = $oOwner.Protection("removeEncryption",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oOwner.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("removeEncryption succeeded; will be saved encrypted: " & $oJsonOut.BoolOf("pending.encrypted") & @CRLF)

;  Sample output:

;  opened with: user
;  removeEncryption was refused: the PDF was not opened with its owner password.
;  opened with: owner
;  removeEncryption succeeded; will be saved encrypted: False