PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Owner
li_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
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
// Load with the current passwords.
loo_Pdf.UserPassword = "secret"
loo_Pdf.OwnerPassword = "owner-secret"
li_Success = loo_Pdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
// The new passwords. Protection reads the properties as they are when it is called.
loo_Pdf.UserPassword = "new-user-secret"
loo_Pdf.OwnerPassword = "new-owner-secret"
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Pdf.Protection("changePasswords",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The algorithm and the permissions are kept.
Write-Debug "algorithm: " + loo_JsonOut.StringOf("pending.algorithm")
Write-Debug "password needed to open: " + string(loo_JsonOut.BoolOf("pending.userPasswordRequired"))
li_Success = loo_Pdf.WriteFile("qa_output/minutes_new_passwords.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The new user password opens the result.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")
loo_Reader.UserPassword = "new-user-secret"
li_Success = loo_Reader.LoadFile("qa_output/minutes_new_passwords.pdf")
if li_Success = 0 then
Write-Debug loo_Reader.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
return
end if
li_Success = loo_Reader.Protection("getInfo",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Reader.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
return
end if
Write-Debug "with the new user password, opened with: " + loo_JsonOut.StringOf("loaded.openedWith")
// And so does the new owner password.
loo_Owner = create oleobject
li_rc = loo_Owner.ConnectToNewObject("Chilkat.Pdf")
loo_Owner.OwnerPassword = "new-owner-secret"
li_Success = loo_Owner.LoadFile("qa_output/minutes_new_passwords.pdf")
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Owner
return
end if
li_Success = loo_Owner.Protection("getInfo",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Owner
return
end if
Write-Debug "with the new owner password, opened with: " + loo_JsonOut.StringOf("loaded.openedWith")
// 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
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Owner