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

Encrypt a PDF for a Certificate Instead of a Password

See more PDF Protection Examples

Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.

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

;  The recipient's certificate.  Only the public certificate is needed to encrypt.
$oRecipient = ObjCreate("Chilkat.Cert")
$bSuccess = $oRecipient.LoadFromFile("qa_data/certs/recipient.cer")
If ($bSuccess = False) Then
    ConsoleWrite($oRecipient.LastErrorText & @CRLF)
    Exit
EndIf

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

;  Call AddEncryptCert once for each recipient.
$bSuccess = $oPdf.AddEncryptCert($oRecipient)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
;  recipient owner rights, so the file can be managed later.
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("method","certificate")
$oJson.UpdateBool("recipientPermissions[0].changeSecurity",True)

$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("method: " & $oJsonOut.StringOf("pending.method") & @CRLF)
ConsoleWrite("algorithm: " & $oJsonOut.StringOf("pending.algorithm") & @CRLF)
ConsoleWrite("recipients: " & $oJsonOut.SizeOfArray("pending.recipients") & @CRLF)

$bSuccess = $oPdf.WriteFile("qa_output/memo_for_recipient.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

;  PreLoadCheck says how the file is protected, without loading it.
$oReader = ObjCreate("Chilkat.Pdf")
$oInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReader.PreLoadCheck("qa_output/memo_for_recipient.pdf",$oInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oReader.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("saved file, method: " & $oInfo.StringOf("method") & @CRLF)
ConsoleWrite("saved file, recipients: " & $oInfo.SizeOfArray("recipients") & @CRLF)

;  The recipient opens it with the certificate and its private key, here from a .pfx file.
;  (On Windows, a certificate that is installed in the Windows certificate store with its private
;  key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
$oMyCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oMyCert.LoadPfxFile("qa_data/pfx/recipient.pfx","pfx-password")
If ($bSuccess = False) Then
    ConsoleWrite($oMyCert.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oReader.SetDecryptCert($oMyCert)
If ($bSuccess = False) Then
    ConsoleWrite($oReader.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oReader.LoadFile("qa_output/memo_for_recipient.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oReader.LastErrorText & @CRLF)
    Exit
EndIf

$oEmpty = ObjCreate("Chilkat.JsonObject")
$oLoadedInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReader.Protection("getInfo",$oEmpty,$oLoadedInfo)
If ($bSuccess = False) Then
    ConsoleWrite($oReader.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("opened with: " & $oLoadedInfo.StringOf("loaded.openedWith") & @CRLF)
ConsoleWrite("pages: " & $oReader.NumPages & @CRLF)

;  Sample output:

;  method: certificate
;  algorithm: aes-256
;  recipients: 1
;  saved file, method: certificate
;  saved file, recipients: 1
;  opened with: certificate
;  pages: 2