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

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

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 two_page_memo.pdf

$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  The passwords are taken from these two properties at the time Protection is called.
;  The user password opens the PDF.  The owner password opens it too, and is the one needed
;  later to change or remove the protection.
$oPdf.UserPassword = "user-secret"
$oPdf.OwnerPassword = "owner-secret"

;  An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
ConsoleWrite("algorithm: " & $oJsonOut.StringOf("pending.algorithm") & @CRLF)
ConsoleWrite("PDF version: " & $oJsonOut.StringOf("pending.pdfVersion") & @CRLF)
ConsoleWrite("password needed to open: " & $oJsonOut.BoolOf("pending.userPasswordRequired") & @CRLF)

;  Writing the file applies it.
$bSuccess = $oPdf.WriteFile("qa_output/memo_encrypted.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  PreLoadCheck reports a file's encryption without loading it, and needs no password.
$oChecker = ObjCreate("Chilkat.Pdf")
$oInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oChecker.PreLoadCheck("qa_output/memo_encrypted.pdf",$oInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oChecker.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("encrypted: " & $oInfo.BoolOf("encrypted") & @CRLF)
ConsoleWrite("openPasswordRequired: " & $oInfo.BoolOf("openPasswordRequired") & @CRLF)

;  With the password, the PDF opens normally.
$oReader = ObjCreate("Chilkat.Pdf")
$oReader.UserPassword = "user-secret"
$bSuccess = $oReader.LoadFile("qa_output/memo_encrypted.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oReader.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("pages: " & $oReader.NumPages & @CRLF)

;  Sample output:

;  algorithm: aes-256
;  PDF version: 1.7
;  password needed to open: True
;  encrypted: True
;  openPasswordRequired: True
;  pages: 2