PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Opened with the user password.
user.i = CkPdf::ckCreate()
If user.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkPdf::setCkUserPassword(user, "secret")
success = CkPdf::ckLoadFile(user,"qa_data/pdf/encrypted_user_password.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(user)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
ProcedureReturn
EndIf
success = CkPdf::ckProtection(user,"getInfo",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(user)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
ProcedureReturn
EndIf
Debug "opened with: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")
; Changing the security needs owner rights, so this is refused.
success = CkPdf::ckProtection(user,"removeEncryption",json,jsonOut)
If success = 0
Debug "removeEncryption was refused: the PDF was not opened with its owner password."
EndIf
; Opened with the owner password.
owner.i = CkPdf::ckCreate()
If owner.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkPdf::setCkOwnerPassword(owner, "owner-secret")
success = CkPdf::ckLoadFile(owner,"qa_data/pdf/encrypted_user_password.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(owner)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
CkPdf::ckDispose(owner)
ProcedureReturn
EndIf
success = CkPdf::ckProtection(owner,"getInfo",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(owner)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
CkPdf::ckDispose(owner)
ProcedureReturn
EndIf
Debug "opened with: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")
; Now it is allowed.
success = CkPdf::ckProtection(owner,"removeEncryption",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(owner)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
CkPdf::ckDispose(owner)
ProcedureReturn
EndIf
Debug "removeEncryption succeeded; will be saved encrypted: " + Str(CkJsonObject::ckBoolOf(jsonOut,"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
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(user)
CkPdf::ckDispose(owner)
ProcedureReturn
EndProcedure