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

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW info;

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

    pdf = CkPdfW_Create();
    info = CkJsonObjectW_Create();

    //  A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
    success = CkPdfW_PreLoadCheck(pdf,L"qa_data/pdf/two_page_memo.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(info);
        return;
    }

    wprintf(L"two_page_memo.pdf\n");
    wprintf(L"  encrypted: %d\n",CkJsonObjectW_BoolOf(info,L"encrypted"));

    //  A PDF that needs a password to open.
    success = CkPdfW_PreLoadCheck(pdf,L"qa_data/pdf/encrypted_user_password.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(info);
        return;
    }

    wprintf(L"encrypted_user_password.pdf\n");
    wprintf(L"  encrypted: %d\n",CkJsonObjectW_BoolOf(info,L"encrypted"));
    wprintf(L"  method: %s\n",CkJsonObjectW_stringOf(info,L"method"));
    wprintf(L"  algorithm: %s\n",CkJsonObjectW_stringOf(info,L"algorithm"));
    wprintf(L"  openPasswordRequired: %d\n",CkJsonObjectW_BoolOf(info,L"openPasswordRequired"));

    //  A PDF that opens without a password but is restricted.  The permissions are readable without
    //  any password.
    success = CkPdfW_PreLoadCheck(pdf,L"qa_data/pdf/no_copy_permission.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(info);
        return;
    }

    wprintf(L"no_copy_permission.pdf\n");
    wprintf(L"  encrypted: %d\n",CkJsonObjectW_BoolOf(info,L"encrypted"));
    wprintf(L"  openPasswordRequired: %d\n",CkJsonObjectW_BoolOf(info,L"openPasswordRequired"));
    wprintf(L"  may copy: %d\n",CkJsonObjectW_BoolOf(info,L"permissions.copy"));

    //  The typical use: ask for a password only when one is needed, then load.
    success = CkPdfW_PreLoadCheck(pdf,L"qa_data/pdf/encrypted_user_password.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(info);
        return;
    }

    if (CkJsonObjectW_BoolOf(info,L"openPasswordRequired") == TRUE) {
        //  An application would prompt for the password here.
        CkPdfW_putUserPassword(pdf,L"secret");
    }

    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/encrypted_user_password.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(info);
        return;
    }

    wprintf(L"loaded, pages: %d\n",CkPdfW_getNumPages(pdf));

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


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(info);

    }