Sample code for 30+ languages & platforms
Xojo Plugin 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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  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

Dim pdf As New Chilkat.Pdf

//  Changing the permissions needs owner rights.  Both passwords are given here, which is what
//  an AES-256 PDF requires for this operation.
pdf.UserPassword = "secret"
pdf.OwnerPassword = "owner-secret"
success = pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

//  No printing and no copying.  Everything not mentioned is granted.
Dim json As New Chilkat.JsonObject
success = json.UpdateString("permissions.print","none")
success = json.UpdateBool("permissions.copy",False)

Dim jsonOut As New Chilkat.JsonObject
success = pdf.Protection("changePermissions",json,jsonOut)
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

System.DebugLog("print: " + jsonOut.StringOf("pending.permissions.print"))
System.DebugLog("copy: " + Str(jsonOut.BoolOf("pending.permissions.copy")))
System.DebugLog("modify: " + Str(jsonOut.BoolOf("pending.permissions.modify")))
System.DebugLog("password still needed to open: " + Str(jsonOut.BoolOf("pending.userPasswordRequired")))

success = pdf.WriteFile("qa_output/minutes_no_print.pdf")
If (success = False) Then
    System.DebugLog(pdf.LastErrorText)
    Return
End If

//  The same user password opens the result, now with the new permissions.
Dim reader As New Chilkat.Pdf
reader.UserPassword = "secret"
success = reader.LoadFile("qa_output/minutes_no_print.pdf")
If (success = False) Then
    System.DebugLog(reader.LastErrorText)
    Return
End If

Dim empty As New Chilkat.JsonObject
Dim info As New Chilkat.JsonObject
success = reader.Protection("getInfo",empty,info)
If (success = False) Then
    System.DebugLog(reader.LastErrorText)
    Return
End If

System.DebugLog("saved file, print: " + info.StringOf("loaded.permissions.print"))
System.DebugLog("saved file, copy: " + Str(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