PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
PDF Owner Password vs. User Password
See more PDF Protection Examples
Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_User
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_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Json
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")
// Opened with the user password.
loo_User = create oleobject
li_rc = loo_User.ConnectToNewObject("Chilkat.Pdf")
loo_User.UserPassword = "secret"
li_Success = loo_User.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if li_Success = 0 then
Write-Debug loo_User.LastErrorText
destroy loo_Json
destroy loo_JsonOut
destroy loo_User
return
end if
li_Success = loo_User.Protection("getInfo",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_User.LastErrorText
destroy loo_Json
destroy loo_JsonOut
destroy loo_User
return
end if
Write-Debug "opened with: " + loo_JsonOut.StringOf("loaded.openedWith")
// Changing the security needs owner rights, so this is refused.
li_Success = loo_User.Protection("removeEncryption",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug "removeEncryption was refused: the PDF was not opened with its owner password."
end if
// Opened with the owner password.
loo_Owner = create oleobject
li_rc = loo_Owner.ConnectToNewObject("Chilkat.Pdf")
loo_Owner.OwnerPassword = "owner-secret"
li_Success = loo_Owner.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Json
destroy loo_JsonOut
destroy loo_User
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_Json
destroy loo_JsonOut
destroy loo_User
destroy loo_Owner
return
end if
Write-Debug "opened with: " + loo_JsonOut.StringOf("loaded.openedWith")
// Now it is allowed.
li_Success = loo_Owner.Protection("removeEncryption",loo_Json,loo_JsonOut)
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Json
destroy loo_JsonOut
destroy loo_User
destroy loo_Owner
return
end if
Write-Debug "removeEncryption succeeded; will be saved encrypted: " + string(loo_JsonOut.BoolOf("pending.encrypted"))
// Sample output:
// opened with: user
// removeEncryption was refused: the PDF was not opened with its owner password.
// opened with: owner
// removeEncryption succeeded; will be saved encrypted: False
destroy loo_Json
destroy loo_JsonOut
destroy loo_User
destroy loo_Owner