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

Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4

See more PDF Protection Examples

Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Info
oleobject loo_Pdf1
oleobject loo_Pdf2
oleobject loo_Pdf3

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_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

//  AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
loo_Pdf1 = create oleobject
li_rc = loo_Pdf1.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf1.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    return
end if

loo_Pdf1.UserPassword = "user-secret"
loo_Pdf1.OwnerPassword = "owner-secret"
loo_Json.UpdateString("algorithmChoice","aes-256")
li_Success = loo_Pdf1.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    return
end if

Write-Debug "aes-256 will be saved as PDF " + loo_JsonOut.StringOf("pending.pdfVersion") + ", Adobe extension level " + string(loo_JsonOut.IntOf("pending.adobeExtensionLevel"))
li_Success = loo_Pdf1.WriteFile("qa_output/memo_aes256.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    return
end if

li_Success = loo_Pdf1.PreLoadCheck("qa_output/memo_aes256.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf1.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    return
end if

Write-Debug "  saved file: " + loo_Info.StringOf("algorithm") + ", PDF " + loo_Info.StringOf("pdfVersion")

//  AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

loo_Pdf2.UserPassword = "user-secret"
loo_Pdf2.OwnerPassword = "owner-secret"
loo_Json.UpdateString("algorithmChoice","aes-128")
li_Success = loo_Pdf2.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

Write-Debug "aes-128 will be saved as PDF " + loo_JsonOut.StringOf("pending.pdfVersion")
li_Success = loo_Pdf2.WriteFile("qa_output/memo_aes128.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

li_Success = loo_Pdf2.PreLoadCheck("qa_output/memo_aes128.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    return
end if

Write-Debug "  saved file: " + loo_Info.StringOf("algorithm") + ", PDF " + loo_Info.StringOf("pdfVersion")

//  RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
loo_Pdf3 = create oleobject
li_rc = loo_Pdf3.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf3.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

loo_Pdf3.UserPassword = "user-secret"
loo_Pdf3.OwnerPassword = "owner-secret"
loo_Json.UpdateString("algorithmChoice","rc4-128")
li_Success = loo_Pdf3.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

Write-Debug "rc4-128 will be saved as PDF " + loo_JsonOut.StringOf("pending.pdfVersion")
li_Success = loo_Pdf3.WriteFile("qa_output/memo_rc4_128.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

li_Success = loo_Pdf3.PreLoadCheck("qa_output/memo_rc4_128.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Info
    destroy loo_Pdf1
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

Write-Debug "  saved file: " + loo_Info.StringOf("algorithm") + ", PDF " + loo_Info.StringOf("pdfVersion")

//  Sample output:

//  aes-256 will be saved as PDF 1.7, Adobe extension level 8
//    saved file: aes-256, PDF 1.7
//  aes-128 will be saved as PDF 1.6
//    saved file: aes-128, PDF 1.6
//  rc4-128 will be saved as PDF 1.4
//    saved file: rc4-128, PDF 1.4


destroy loo_Json
destroy loo_JsonOut
destroy loo_Info
destroy loo_Pdf1
destroy loo_Pdf2
destroy loo_Pdf3