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

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    HCkPdfW checker;
    HCkJsonObjectW info;
    HCkPdfW reader;

    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

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

    //  The passwords are taken from these two properties at the time Protection is called.
    //  The user password opens the PDF.  The owner password opens it too, and is the one needed
    //  later to change or remove the protection.
    CkPdfW_putUserPassword(pdf,L"user-secret");
    CkPdfW_putOwnerPassword(pdf,L"owner-secret");

    //  An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();
    success = CkPdfW_Protection(pdf,L"encrypt",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    //  Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
    wprintf(L"algorithm: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.algorithm"));
    wprintf(L"PDF version: %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.pdfVersion"));
    wprintf(L"password needed to open: %d\n",CkJsonObjectW_BoolOf(jsonOut,L"pending.userPasswordRequired"));

    //  Writing the file applies it.
    success = CkPdfW_WriteFile(pdf,L"qa_output/memo_encrypted.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    //  PreLoadCheck reports a file's encryption without loading it, and needs no password.
    checker = CkPdfW_Create();
    info = CkJsonObjectW_Create();
    success = CkPdfW_PreLoadCheck(checker,L"qa_output/memo_encrypted.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(checker));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkPdfW_Dispose(checker);
        CkJsonObjectW_Dispose(info);
        return;
    }

    wprintf(L"encrypted: %d\n",CkJsonObjectW_BoolOf(info,L"encrypted"));
    wprintf(L"openPasswordRequired: %d\n",CkJsonObjectW_BoolOf(info,L"openPasswordRequired"));

    //  With the password, the PDF opens normally.
    reader = CkPdfW_Create();
    CkPdfW_putUserPassword(reader,L"user-secret");
    success = CkPdfW_LoadFile(reader,L"qa_output/memo_encrypted.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(reader));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkPdfW_Dispose(checker);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(reader);
        return;
    }

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

    //  Sample output:

    //  algorithm: aes-256
    //  PDF version: 1.7
    //  password needed to open: True
    //  encrypted: True
    //  openPasswordRequired: True
    //  pages: 2


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);
    CkPdfW_Dispose(checker);
    CkJsonObjectW_Dispose(info);
    CkPdfW_Dispose(reader);

    }