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

Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4

See more PDF Protection Examples

Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    HCkJsonObjectW info;
    HCkPdfW pdf1;
    HCkPdfW pdf2;
    HCkPdfW pdf3;

    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

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();
    info = CkJsonObjectW_Create();

    //  AES-256, the default.  Saved as PDF 1.7 with Adobe extension level 8.
    pdf1 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf1,L"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf1));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        return;
    }

    CkPdfW_putUserPassword(pdf1,L"user-secret");
    CkPdfW_putOwnerPassword(pdf1,L"owner-secret");
    CkJsonObjectW_UpdateString(json,L"algorithmChoice",L"aes-256");
    success = CkPdfW_Protection(pdf1,L"encrypt",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf1));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        return;
    }

    wprintf(L"aes-256 will be saved as PDF %s, Adobe extension level %d\n",CkJsonObjectW_stringOf(jsonOut,L"pending.pdfVersion"),CkJsonObjectW_IntOf(jsonOut,L"pending.adobeExtensionLevel"));
    success = CkPdfW_WriteFile(pdf1,L"qa_output/memo_aes256.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf1));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        return;
    }

    success = CkPdfW_PreLoadCheck(pdf1,L"qa_output/memo_aes256.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf1));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        return;
    }

    wprintf(L"  saved file: %s, PDF %s\n",CkJsonObjectW_stringOf(info,L"algorithm"),CkJsonObjectW_stringOf(info,L"pdfVersion"));

    //  AES-128, for readers that predate AES-256.  Saved as PDF 1.6.
    pdf2 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf2,L"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        return;
    }

    CkPdfW_putUserPassword(pdf2,L"user-secret");
    CkPdfW_putOwnerPassword(pdf2,L"owner-secret");
    CkJsonObjectW_UpdateString(json,L"algorithmChoice",L"aes-128");
    success = CkPdfW_Protection(pdf2,L"encrypt",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        return;
    }

    wprintf(L"aes-128 will be saved as PDF %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.pdfVersion"));
    success = CkPdfW_WriteFile(pdf2,L"qa_output/memo_aes128.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        return;
    }

    success = CkPdfW_PreLoadCheck(pdf2,L"qa_output/memo_aes128.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf2));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        return;
    }

    wprintf(L"  saved file: %s, PDF %s\n",CkJsonObjectW_stringOf(info,L"algorithm"),CkJsonObjectW_stringOf(info,L"pdfVersion"));

    //  RC4-128, a broken cipher, only for readers too old for AES.  Saved as PDF 1.4.
    pdf3 = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf3,L"qa_data/pdf/two_page_memo.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf3));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        CkPdfW_Dispose(pdf3);
        return;
    }

    CkPdfW_putUserPassword(pdf3,L"user-secret");
    CkPdfW_putOwnerPassword(pdf3,L"owner-secret");
    CkJsonObjectW_UpdateString(json,L"algorithmChoice",L"rc4-128");
    success = CkPdfW_Protection(pdf3,L"encrypt",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf3));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        CkPdfW_Dispose(pdf3);
        return;
    }

    wprintf(L"rc4-128 will be saved as PDF %s\n",CkJsonObjectW_stringOf(jsonOut,L"pending.pdfVersion"));
    success = CkPdfW_WriteFile(pdf3,L"qa_output/memo_rc4_128.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf3));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        CkPdfW_Dispose(pdf3);
        return;
    }

    success = CkPdfW_PreLoadCheck(pdf3,L"qa_output/memo_rc4_128.pdf",info);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf3));
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        CkJsonObjectW_Dispose(info);
        CkPdfW_Dispose(pdf1);
        CkPdfW_Dispose(pdf2);
        CkPdfW_Dispose(pdf3);
        return;
    }

    wprintf(L"  saved file: %s, PDF %s\n",CkJsonObjectW_stringOf(info,L"algorithm"),CkJsonObjectW_stringOf(info,L"pdfVersion"));

    //  Sample output:

    //  aes-256 will be saved as PDF 1.7, Adobe extension level 8
    //    saved file: aes-256, PDF 1.7
    //  aes-128 will be saved as PDF 1.6
    //    saved file: aes-128, PDF 1.6
    //  rc4-128 will be saved as PDF 1.4
    //    saved file: rc4-128, PDF 1.4


    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);
    CkJsonObjectW_Dispose(info);
    CkPdfW_Dispose(pdf1);
    CkPdfW_Dispose(pdf2);
    CkPdfW_Dispose(pdf3);

    }