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

Remove the Restrictions from a PDF

See more PDF Protection Examples

Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Opts
oleobject loo_ExtractOut
oleobject loo_Sb

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

//  This PDF opens without a password, but copying text from it is not permitted.
//  Removing the restrictions needs owner rights: load with the owner password.
loo_Pdf.OwnerPassword = "owner-secret"
li_Success = loo_Pdf.LoadFile("qa_data/pdf/no_copy_permission.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 "may copy, before: " + string(loo_JsonOut.BoolOf("loaded.permissions.copy"))
Write-Debug "password needed to open: " + string(loo_JsonOut.BoolOf("loaded.userPasswordRequired"))

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

//  With no user password and nothing restricted there is nothing left to protect, so the PDF
//  will be saved unencrypted.  A PDF that needs a password to open would stay encrypted and
//  keep its password.
Write-Debug "will be saved encrypted: " + string(loo_JsonOut.BoolOf("pending.encrypted"))

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

//  Anyone can now extract the text.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")

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

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

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

loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Reader.ExtractTextSb("text",loo_Opts,loo_Sb,loo_ExtractOut)
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Opts
    destroy loo_ExtractOut
    destroy loo_Sb
    return
end if

Write-Debug "characters extracted: " + string(loo_Sb.Length)

//  Sample output:

//  may copy, before: False
//  password needed to open: False
//  will be saved encrypted: False
//  characters extracted: 216


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Opts
destroy loo_ExtractOut
destroy loo_Sb