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

C
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;

    success = FALSE;

    //  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

    pdf = CkPdf_Create();
    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();

    //  The PDF loads without its password: the file's structure is readable, so properties such as
    //  NumPages and the security settings are available...
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  ...but the content cannot be decrypted, so extracting text fails.
    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("Cannot extract: %s\n",CkPdf_lastErrorText(pdf));
    }

    //  "assess" still works: it reports that a password is required and nothing else.
    success = CkPdf_ExtractText(pdf,"assess",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("passwordRequired: %d\n",CkJsonObject_BoolOf(jsonOut,"quality.passwordRequired"));
    printf("rating: %s\n",CkJsonObject_stringOf(jsonOut,"quality.rating"));

    //  Set the password and load again.
    CkPdf_putUserPassword(pdf,"secret");
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("%s\n",CkJsonObject_stringOf(jsonOut,"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.


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);

    }