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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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

set pdf = Server.CreateObject("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 = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  No printing and no copying.  Everything not mentioned is granted.
set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("permissions.print","none")
success = json.UpdateBool("permissions.copy",0)

set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Protection("changePermissions",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "print: " & jsonOut.StringOf("pending.permissions.print")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "copy: " & jsonOut.BoolOf("pending.permissions.copy")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "modify: " & jsonOut.BoolOf("pending.permissions.modify")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "password still needed to open: " & jsonOut.BoolOf("pending.userPasswordRequired")) & "</pre>"

success = pdf.WriteFile("qa_output/minutes_no_print.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  The same user password opens the result, now with the new permissions.
set reader = Server.CreateObject("Chilkat.Pdf")
reader.UserPassword = "secret"
success = reader.LoadFile("qa_output/minutes_no_print.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

set empty = Server.CreateObject("Chilkat.JsonObject")
set info = Server.CreateObject("Chilkat.JsonObject")
success = reader.Protection("getInfo",empty,info)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "saved file, print: " & info.StringOf("loaded.permissions.print")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "saved file, copy: " & info.BoolOf("loaded.permissions.copy")) & "</pre>"

'  Sample output:

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

%>
</body>
</html>