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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoUser
    Handle hoOwner
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  Opened with the user password.
    Get Create (RefClass(cComChilkatPdf)) To hoUser
    If (Not(IsComObjectCreated(hoUser))) Begin
        Send CreateComObject of hoUser
    End
    Set ComUserPassword Of hoUser To "secret"
    Get ComLoadFile Of hoUser "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoUser To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoUser "getInfo" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoUser To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1

    //  Changing the security needs owner rights, so this is refused.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoUser "removeEncryption" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Showln "removeEncryption was refused: the PDF was not opened with its owner password."
    End

    //  Opened with the owner password.
    Get Create (RefClass(cComChilkatPdf)) To hoOwner
    If (Not(IsComObjectCreated(hoOwner))) Begin
        Send CreateComObject of hoOwner
    End
    Set ComOwnerPassword Of hoOwner To "owner-secret"
    Get ComLoadFile Of hoOwner "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoOwner "getInfo" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1

    //  Now it is allowed.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoOwner "removeEncryption" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "pending.encrypted" To bTemp1
    Showln "removeEncryption succeeded; will be saved encrypted: " bTemp1

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


End_Procedure