Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oReader
LOCAL oEmpty
LOCAL oInfo
nSuccess := 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
oPdf := 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.
oPdf:UserPassword := "secret"
oPdf:OwnerPassword := "owner-secret"
nSuccess := oPdf:LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
// No printing and no copying. Everything not mentioned is granted.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("permissions.print", "none")
oJson:UpdateBool("permissions.copy", 0)
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("changePermissions", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "print: " + oJsonOut:StringOf("pending.permissions.print")
? "copy: " + Str(oJsonOut:BoolOf("pending.permissions.copy"))
? "modify: " + Str(oJsonOut:BoolOf("pending.permissions.modify"))
? "password still needed to open: " + Str(oJsonOut:BoolOf("pending.userPasswordRequired"))
nSuccess := oPdf:WriteFile("qa_output/minutes_no_print.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// The same user password opens the result, now with the new permissions.
oReader := CreateObject("Chilkat.Pdf")
oReader:UserPassword := "secret"
nSuccess := oReader:LoadFile("qa_output/minutes_no_print.pdf")
IF (nSuccess == 0)
? oReader:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
RETURN
ENDIF
oEmpty := CreateObject("Chilkat.JsonObject")
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oReader:Protection("getInfo", oEmpty, oInfo)
IF (nSuccess == 0)
? oReader:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
RETURN
ENDIF
? "saved file, print: " + oInfo:StringOf("loaded.permissions.print")
? "saved file, copy: " + Str(oInfo: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
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()