PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Checker
oleobject loo_Info
oleobject loo_Reader
li_Success = 0
// 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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
// 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.
loo_Pdf.UserPassword = "user-secret"
loo_Pdf.OwnerPassword = "owner-secret"
// An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Pdf.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
Write-Debug "algorithm: " + loo_JsonOut.StringOf("pending.algorithm")
Write-Debug "PDF version: " + loo_JsonOut.StringOf("pending.pdfVersion")
Write-Debug "password needed to open: " + string(loo_JsonOut.BoolOf("pending.userPasswordRequired"))
// Writing the file applies it.
li_Success = loo_Pdf.WriteFile("qa_output/memo_encrypted.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// PreLoadCheck reports a file's encryption without loading it, and needs no password.
loo_Checker = create oleobject
li_rc = loo_Checker.ConnectToNewObject("Chilkat.Pdf")
loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Checker.PreLoadCheck("qa_output/memo_encrypted.pdf",loo_Info)
if li_Success = 0 then
Write-Debug loo_Checker.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Checker
destroy loo_Info
return
end if
Write-Debug "encrypted: " + string(loo_Info.BoolOf("encrypted"))
Write-Debug "openPasswordRequired: " + string(loo_Info.BoolOf("openPasswordRequired"))
// With the password, the PDF opens normally.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")
loo_Reader.UserPassword = "user-secret"
li_Success = loo_Reader.LoadFile("qa_output/memo_encrypted.pdf")
if li_Success = 0 then
Write-Debug loo_Reader.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Checker
destroy loo_Info
destroy loo_Reader
return
end if
Write-Debug "pages: " + string(loo_Reader.NumPages)
// Sample output:
// algorithm: aes-256
// PDF version: 1.7
// password needed to open: True
// encrypted: True
// openPasswordRequired: True
// pages: 2
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Checker
destroy loo_Info
destroy loo_Reader