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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

Chilkat C Downloads

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

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

    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 encrypted_user_password.pdf

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();

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

    success = CkPdf_Protection(user,"getInfo",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(user));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(user);
        return;
    }

    printf("opened with: %s\n",CkJsonObject_stringOf(jsonOut,"loaded.openedWith"));

    //  Changing the security needs owner rights, so this is refused.
    success = CkPdf_Protection(user,"removeEncryption",json,jsonOut);
    if (success == FALSE) {
        printf("removeEncryption was refused: the PDF was not opened with its owner password.\n");
    }

    //  Opened with the owner password.
    owner = CkPdf_Create();
    CkPdf_putOwnerPassword(owner,"owner-secret");
    success = CkPdf_LoadFile(owner,"qa_data/pdf/encrypted_user_password.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(owner));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(user);
        CkPdf_Dispose(owner);
        return;
    }

    success = CkPdf_Protection(owner,"getInfo",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(owner));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(user);
        CkPdf_Dispose(owner);
        return;
    }

    printf("opened with: %s\n",CkJsonObject_stringOf(jsonOut,"loaded.openedWith"));

    //  Now it is allowed.
    success = CkPdf_Protection(owner,"removeEncryption",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(owner));
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(user);
        CkPdf_Dispose(owner);
        return;
    }

    printf("removeEncryption succeeded; will be saved encrypted: %d\n",CkJsonObject_BoolOf(jsonOut,"pending.encrypted"));

    //  Sample output:

    //  opened with: user
    //  removeEncryption was refused: the PDF was not opened with its owner password.
    //  opened with: owner
    //  removeEncryption succeeded; will be saved encrypted: False


    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkPdf_Dispose(user);
    CkPdf_Dispose(owner);

    }