AutoIt Requires Chilkat v11.7.0+
AutoIt
Find Out Whether a PDF Needs a Password Before Loading It
See more PDF Protection Examples
Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.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 two_page_memo.pdf
; The PDF used in this example is available at encrypted_user_password.pdf
; The PDF used in this example is available at no_copy_permission.pdf
$oPdf = ObjCreate("Chilkat.Pdf")
$oInfo = ObjCreate("Chilkat.JsonObject")
; A PDF that is not encrypted. Nothing but "encrypted" is reported for it.
$bSuccess = $oPdf.PreLoadCheck("qa_data/pdf/two_page_memo.pdf",$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("two_page_memo.pdf" & @CRLF)
ConsoleWrite(" encrypted: " & $oInfo.BoolOf("encrypted") & @CRLF)
; A PDF that needs a password to open.
$bSuccess = $oPdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("encrypted_user_password.pdf" & @CRLF)
ConsoleWrite(" encrypted: " & $oInfo.BoolOf("encrypted") & @CRLF)
ConsoleWrite(" method: " & $oInfo.StringOf("method") & @CRLF)
ConsoleWrite(" algorithm: " & $oInfo.StringOf("algorithm") & @CRLF)
ConsoleWrite(" openPasswordRequired: " & $oInfo.BoolOf("openPasswordRequired") & @CRLF)
; A PDF that opens without a password but is restricted. The permissions are readable without
; any password.
$bSuccess = $oPdf.PreLoadCheck("qa_data/pdf/no_copy_permission.pdf",$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("no_copy_permission.pdf" & @CRLF)
ConsoleWrite(" encrypted: " & $oInfo.BoolOf("encrypted") & @CRLF)
ConsoleWrite(" openPasswordRequired: " & $oInfo.BoolOf("openPasswordRequired") & @CRLF)
ConsoleWrite(" may copy: " & $oInfo.BoolOf("permissions.copy") & @CRLF)
; The typical use: ask for a password only when one is needed, then load.
$bSuccess = $oPdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
If ($oInfo.BoolOf("openPasswordRequired") = True) Then
; An application would prompt for the password here.
$oPdf.UserPassword = "secret"
EndIf
$bSuccess = $oPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("loaded, pages: " & $oPdf.NumPages & @CRLF)
; Sample output:
; two_page_memo.pdf
; encrypted: False
; encrypted_user_password.pdf
; encrypted: True
; method: password
; algorithm: rc4-128
; openPasswordRequired: True
; no_copy_permission.pdf
; encrypted: True
; openPasswordRequired: False
; may copy: False
; loaded, pages: 1