Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.7.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

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

//  The password must be set before loading.
loo_Pdf.UserPassword = "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

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("getInfo",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

loo_JsonOut.EmitCompact = 0
Write-Debug loo_JsonOut.Emit()

//  "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.
Write-Debug "algorithm: " + loo_JsonOut.StringOf("loaded.algorithm")
Write-Debug "opened with: " + loo_JsonOut.StringOf("loaded.openedWith")
Write-Debug "password needed to open: " + string(loo_JsonOut.BoolOf("loaded.userPasswordRequired"))
Write-Debug "may print: " + loo_JsonOut.StringOf("loaded.permissions.print")
Write-Debug "may copy: " + string(loo_JsonOut.BoolOf("loaded.permissions.copy"))
Write-Debug "changed: " + string(loo_JsonOut.BoolOf("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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut