DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoReader
Variant vEmpty
Handle hoEmpty
Variant vInfo
Handle hoInfo
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
// Changing the permissions needs owner rights. Both passwords are given here, which is what
// an AES-256 PDF requires for this operation.
Set ComUserPassword Of hoPdf To "secret"
Set ComOwnerPassword Of hoPdf To "owner-secret"
Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// No printing and no copying. Everything not mentioned is granted.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "permissions.print" "none" To iSuccess
Get ComUpdateBool Of hoJson "permissions.copy" False To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComProtection Of hoPdf "changePermissions" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "pending.permissions.print" To sTemp1
Showln "print: " sTemp1
Get ComBoolOf Of hoJsonOut "pending.permissions.copy" To bTemp1
Showln "copy: " bTemp1
Get ComBoolOf Of hoJsonOut "pending.permissions.modify" To bTemp1
Showln "modify: " bTemp1
Get ComBoolOf Of hoJsonOut "pending.userPasswordRequired" To bTemp1
Showln "password still needed to open: " bTemp1
Get ComWriteFile Of hoPdf "qa_output/minutes_no_print.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// The same user password opens the result, now with the new permissions.
Get Create (RefClass(cComChilkatPdf)) To hoReader
If (Not(IsComObjectCreated(hoReader))) Begin
Send CreateComObject of hoReader
End
Set ComUserPassword Of hoReader To "secret"
Get ComLoadFile Of hoReader "qa_output/minutes_no_print.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoReader To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoEmpty
If (Not(IsComObjectCreated(hoEmpty))) Begin
Send CreateComObject of hoEmpty
End
Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
If (Not(IsComObjectCreated(hoInfo))) Begin
Send CreateComObject of hoInfo
End
Get pvComObject of hoEmpty to vEmpty
Get pvComObject of hoInfo to vInfo
Get ComProtection Of hoReader "getInfo" vEmpty vInfo To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoReader To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoInfo "loaded.permissions.print" To sTemp1
Showln "saved file, print: " sTemp1
Get ComBoolOf Of hoInfo "loaded.permissions.copy" To bTemp1
Showln "saved file, copy: " bTemp1
// Sample output:
// print: none
// copy: False
// modify: True
// password still needed to open: True
// saved file, print: none
// saved file, copy: False
End_Procedure