DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJson
Out Handle hoJsonOut
Handle hoChecker
Variant vInfo
Handle hoInfo
Handle hoReader
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/two_page_memo.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Set ComUserPassword Of hoPdf To "user-secret"
Set ComOwnerPassword Of hoPdf To "owner-secret"
// An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComProtection Of hoPdf "encrypt" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
Get ComStringOf Of hoJsonOut "pending.algorithm" To sTemp1
Showln "algorithm: " sTemp1
Get ComStringOf Of hoJsonOut "pending.pdfVersion" To sTemp1
Showln "PDF version: " sTemp1
Get ComBoolOf Of hoJsonOut "pending.userPasswordRequired" To bTemp1
Showln "password needed to open: " bTemp1
// Writing the file applies it.
Get ComWriteFile Of hoPdf "qa_output/memo_encrypted.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// PreLoadCheck reports a file's encryption without loading it, and needs no password.
Get Create (RefClass(cComChilkatPdf)) To hoChecker
If (Not(IsComObjectCreated(hoChecker))) Begin
Send CreateComObject of hoChecker
End
Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
If (Not(IsComObjectCreated(hoInfo))) Begin
Send CreateComObject of hoInfo
End
Get pvComObject of hoInfo to vInfo
Get ComPreLoadCheck Of hoChecker "qa_output/memo_encrypted.pdf" vInfo To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoChecker To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComBoolOf Of hoInfo "encrypted" To bTemp1
Showln "encrypted: " bTemp1
Get ComBoolOf Of hoInfo "openPasswordRequired" To bTemp1
Showln "openPasswordRequired: " bTemp1
// With the password, the PDF opens normally.
Get Create (RefClass(cComChilkatPdf)) To hoReader
If (Not(IsComObjectCreated(hoReader))) Begin
Send CreateComObject of hoReader
End
Set ComUserPassword Of hoReader To "user-secret"
Get ComLoadFile Of hoReader "qa_output/memo_encrypted.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoReader To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComNumPages Of hoReader To iTemp1
Showln "pages: " iTemp1
// Sample output:
// algorithm: aes-256
// PDF version: 1.7
// password needed to open: True
// encrypted: True
// openPasswordRequired: True
// pages: 2
End_Procedure