DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
// The PDF loads without its password: the file's structure is readable, so properties such as
// NumPages and the security settings are available...
Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// ...but the content cannot be decrypted, so extracting text fails.
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln "Cannot extract: " sTemp1
End
// "assess" still works: it reports that a password is required and nothing else.
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "assess" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComBoolOf Of hoJsonOut "quality.passwordRequired" To bTemp1
Showln "passwordRequired: " bTemp1
Get ComStringOf Of hoJsonOut "quality.rating" To sTemp1
Showln "rating: " sTemp1
// Set the password and load again.
Set ComUserPassword Of hoPdf To "secret"
Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln sTemp1
// 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.
End_Procedure