DataFlex Requires Chilkat v11.7.0+
DataFlex
Change the Passwords of an Encrypted PDF
See more PDF Protection Examples
Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
Handle hoReader
Handle hoOwner
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
// Load with the current passwords.
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
// The new passwords. Protection reads the properties as they are when it is called.
Set ComUserPassword Of hoPdf To "new-user-secret"
Set ComOwnerPassword Of hoPdf To "new-owner-secret"
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
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 "changePasswords" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// The algorithm and the permissions are kept.
Get ComStringOf Of hoJsonOut "pending.algorithm" To sTemp1
Showln "algorithm: " sTemp1
Get ComBoolOf Of hoJsonOut "pending.userPasswordRequired" To bTemp1
Showln "password needed to open: " bTemp1
Get ComWriteFile Of hoPdf "qa_output/minutes_new_passwords.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// The new user password opens the result.
Get Create (RefClass(cComChilkatPdf)) To hoReader
If (Not(IsComObjectCreated(hoReader))) Begin
Send CreateComObject of hoReader
End
Set ComUserPassword Of hoReader To "new-user-secret"
Get ComLoadFile Of hoReader "qa_output/minutes_new_passwords.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoReader To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComProtection Of hoReader "getInfo" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoReader To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
Showln "with the new user password, opened with: " sTemp1
// And so does the new owner password.
Get Create (RefClass(cComChilkatPdf)) To hoOwner
If (Not(IsComObjectCreated(hoOwner))) Begin
Send CreateComObject of hoOwner
End
Set ComOwnerPassword Of hoOwner To "new-owner-secret"
Get ComLoadFile Of hoOwner "qa_output/minutes_new_passwords.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoOwner To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComProtection Of hoOwner "getInfo" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoOwner To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
Showln "with the new owner password, opened with: " sTemp1
// Sample output:
// algorithm: rc4-128
// password needed to open: True
// with the new user password, opened with: user
// with the new owner password, opened with: owner
End_Procedure