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

Extract Text from a Password-Protected PDF

See more PDF Text Extraction Examples

Demonstrates extracting text from a PDF that requires a password to open. The PDF loads without the password (its structure can be read), but the "text" operation fails because the content cannot be decrypted; "assess" reports that a password is required. With the UserPassword property set before loading, extraction works normally.

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 (user password: secret) 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
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  The PDF loads without its password: the file's structure is readable, so properties such as
//  NumPages and the security settings are available...
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_Json
    destroy loo_JsonOut
    return
end if

//  ...but the content cannot be decrypted, so extracting text fails.
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug "Cannot extract: " + loo_Pdf.LastErrorText
end if

//  "assess" still works: it reports that a password is required and nothing else.
li_Success = loo_Pdf.ExtractText("assess",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

Write-Debug "passwordRequired: " + string(loo_JsonOut.BoolOf("quality.passwordRequired"))
Write-Debug "rating: " + loo_JsonOut.StringOf("quality.rating")

//  Set the password and load again.
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
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

li_Success = loo_Pdf.ExtractText("text",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

Write-Debug loo_JsonOut.StringOf("text")

//  Sample output:

//  Cannot extract: ChilkatLog:
//    ExtractText:
//      ...
//        The PDF is encrypted and could not be opened (wrong or missing password); its text cannot be read.
//      ...
//  passwordRequired: true
//  rating: none
//  Board Minutes (Confidential)
//  
//  The board met on 3 November 2026. Present: the chair, the treasurer and four members. The treasurer reported that spending for the season came in under budget.
//  
//  The meeting closed at 8.45 p.m.


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut