Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut

lnSuccess = 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

loPdf = CreateObject('Chilkat.Pdf')
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('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...
lnSuccess = loPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  ...but the content cannot be decrypted, so extracting text fails.
lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? "Cannot extract: " + loPdf.LastErrorText
ENDIF

*  "assess" still works: it reports that a password is required and nothing else.
lnSuccess = loPdf.ExtractText("assess",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "passwordRequired: " + STR(loJsonOut.BoolOf("quality.passwordRequired"))
? "rating: " + loJsonOut.StringOf("quality.rating")

*  Set the password and load again.
loPdf.UserPassword = "secret"
lnSuccess = loPdf.LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

lnSuccess = loPdf.ExtractText("text",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? loJsonOut.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.

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut