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

Remove the Password and Encryption from a PDF

See more PDF Protection Examples

Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Info

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 encrypted_user_password.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

//  Removing the encryption needs owner rights: load with the owner password.
loo_Pdf.OwnerPassword = "owner-secret"
li_Success = loo_Pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

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("getInfo",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

Write-Debug "encrypted, as loaded: " + string(loo_JsonOut.BoolOf("loaded.encrypted"))
Write-Debug "algorithm: " + loo_JsonOut.StringOf("loaded.algorithm")

li_Success = loo_Pdf.Protection("removeEncryption",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

Write-Debug "will be saved encrypted: " + string(loo_JsonOut.BoolOf("pending.encrypted"))

li_Success = loo_Pdf.WriteFile("qa_output/minutes_unprotected.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  The result is an ordinary PDF that opens without a password.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")

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

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

Write-Debug "encrypted: " + string(loo_Info.BoolOf("encrypted"))
li_Success = loo_Reader.LoadFile("qa_output/minutes_unprotected.pdf")
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    return
end if

Write-Debug "pages: " + string(loo_Reader.NumPages)

//  Sample output:

//  encrypted, as loaded: True
//  algorithm: rc4-128
//  will be saved encrypted: False
//  encrypted: False
//  pages: 1


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Info