PureBasic Requires Chilkat v11.7.0+
PureBasic
Get the Encryption and Permissions of a Loaded PDF
See more PDF Protection Examples
Demonstrates the getInfo operation of Protection, which reports how the loaded PDF is protected: the method and algorithm, whether a password is needed to open it, how it was opened (with the user password, the owner password, an empty password, or a certificate), and each permission. The loaded member describes the file as it is, the pending member describes the security it will be saved with, and changed says whether the two differ. getInfo is the one operation that does not need owner rights.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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; The password must be set before loading.
CkPdf::setCkUserPassword(pdf, "secret")
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
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
success = CkPdf::ckProtection(pdf,"getInfo",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(jsonOut, 0)
Debug CkJsonObject::ckEmit(jsonOut)
; "loaded" is the security the file has. "pending" is the security it will be saved with, which
; is the same until an operation such as encrypt or changePermissions changes it.
Debug "algorithm: " + CkJsonObject::ckStringOf(jsonOut,"loaded.algorithm")
Debug "opened with: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")
Debug "password needed to open: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.userPasswordRequired"))
Debug "may print: " + CkJsonObject::ckStringOf(jsonOut,"loaded.permissions.print")
Debug "may copy: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.permissions.copy"))
Debug "changed: " + Str(CkJsonObject::ckBoolOf(jsonOut,"changed"))
; Sample output:
; {
; "changed": false,
; "loaded": {
; "encrypted": true,
; "method": "password",
; "algorithm": "rc4-128",
; "pdfVersion": "1.3",
; "scope": "all",
; "userPasswordRequired": true,
; "ownerPasswordSet": true,
; "openedWith": "user",
; "permissions": {
; "print": "full",
; "modify": true,
; "copy": true,
; "accessibility": true,
; "annotate": true,
; "fillForms": true,
; "assemble": true
; }
; },
; "pending": {
; "encrypted": true,
; "method": "password",
; "algorithm": "rc4-128",
; "pdfVersion": "1.3",
; "scope": "all",
; "userPasswordRequired": true,
; "ownerPasswordSet": true,
; "permissions": {
; "print": "full",
; "modify": true,
; "copy": true,
; "accessibility": true,
; "annotate": true,
; "fillForms": true,
; "assemble": true
; }
; }
; }
;
; algorithm: rc4-128
; opened with: user
; password needed to open: True
; may print: full
; may copy: True
; changed: False
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure