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

Find Out Whether a PDF Needs a Password Before Loading It

See more PDF Protection Examples

Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Info

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 two_page_memo.pdf

//  The PDF used in this example is available at encrypted_user_password.pdf

//  The PDF used in this example is available at no_copy_permission.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
loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

//  A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
li_Success = loo_Pdf.PreLoadCheck("qa_data/pdf/two_page_memo.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Info
    return
end if

Write-Debug "two_page_memo.pdf"
Write-Debug "  encrypted: " + string(loo_Info.BoolOf("encrypted"))

//  A PDF that needs a password to open.
li_Success = loo_Pdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Info
    return
end if

Write-Debug "encrypted_user_password.pdf"
Write-Debug "  encrypted: " + string(loo_Info.BoolOf("encrypted"))
Write-Debug "  method: " + loo_Info.StringOf("method")
Write-Debug "  algorithm: " + loo_Info.StringOf("algorithm")
Write-Debug "  openPasswordRequired: " + string(loo_Info.BoolOf("openPasswordRequired"))

//  A PDF that opens without a password but is restricted.  The permissions are readable without
//  any password.
li_Success = loo_Pdf.PreLoadCheck("qa_data/pdf/no_copy_permission.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Info
    return
end if

Write-Debug "no_copy_permission.pdf"
Write-Debug "  encrypted: " + string(loo_Info.BoolOf("encrypted"))
Write-Debug "  openPasswordRequired: " + string(loo_Info.BoolOf("openPasswordRequired"))
Write-Debug "  may copy: " + string(loo_Info.BoolOf("permissions.copy"))

//  The typical use: ask for a password only when one is needed, then load.
li_Success = loo_Pdf.PreLoadCheck("qa_data/pdf/encrypted_user_password.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Info
    return
end if

if loo_Info.BoolOf("openPasswordRequired") = 1 then
    //  An application would prompt for the password here.
    loo_Pdf.UserPassword = "secret"
end if

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
    destroy loo_Info
    return
end if

Write-Debug "loaded, pages: " + string(loo_Pdf.NumPages)

//  Sample output:

//  two_page_memo.pdf
//    encrypted: False
//  encrypted_user_password.pdf
//    encrypted: True
//    method: password
//    algorithm: rc4-128
//    openPasswordRequired: True
//  no_copy_permission.pdf
//    encrypted: True
//    openPasswordRequired: False
//    may copy: False
//  loaded, pages: 1


destroy loo_Pdf
destroy loo_Info