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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Empty
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

//  Changing the permissions needs owner rights.  Both passwords are given here, which is what
//  an AES-256 PDF requires for this operation.
loo_Pdf.UserPassword = "secret"
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

//  No printing and no copying.  Everything not mentioned is granted.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("permissions.print","none")
loo_Json.UpdateBool("permissions.copy",0)

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

li_Success = loo_Pdf.Protection("changePermissions",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 "print: " + loo_JsonOut.StringOf("pending.permissions.print")
Write-Debug "copy: " + string(loo_JsonOut.BoolOf("pending.permissions.copy"))
Write-Debug "modify: " + string(loo_JsonOut.BoolOf("pending.permissions.modify"))
Write-Debug "password still needed to open: " + string(loo_JsonOut.BoolOf("pending.userPasswordRequired"))

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

//  The same user password opens the result, now with the new permissions.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")

loo_Reader.UserPassword = "secret"
li_Success = loo_Reader.LoadFile("qa_output/minutes_no_print.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_Empty = create oleobject
li_rc = loo_Empty.ConnectToNewObject("Chilkat.JsonObject")

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

li_Success = loo_Reader.Protection("getInfo",loo_Empty,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_Empty
    destroy loo_Info
    return
end if

Write-Debug "saved file, print: " + loo_Info.StringOf("loaded.permissions.print")
Write-Debug "saved file, copy: " + string(loo_Info.BoolOf("loaded.permissions.copy"))

//  Sample output:

//  print: none
//  copy: False
//  modify: True
//  password still needed to open: True
//  saved file, print: none
//  saved file, copy: False


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Empty
destroy loo_Info