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

Remove the Password and Encryption from a PDF

See more PDF Protection Examples

Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoReader
    Variant vInfo
    Handle hoInfo
    String sTemp1
    Integer iTemp1
    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(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End

    //  Removing the encryption needs owner rights: load with the owner password.
    Set ComOwnerPassword Of hoPdf To "owner-secret"
    Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    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
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoPdf "getInfo" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "loaded.encrypted" To bTemp1
    Showln "encrypted, as loaded: " bTemp1
    Get ComStringOf Of hoJsonOut "loaded.algorithm" To sTemp1
    Showln "algorithm: " sTemp1

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

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

    Get ComWriteFile Of hoPdf "qa_output/minutes_unprotected.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The result is an ordinary PDF that opens without a password.
    Get Create (RefClass(cComChilkatPdf)) To hoReader
    If (Not(IsComObjectCreated(hoReader))) Begin
        Send CreateComObject of hoReader
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
    If (Not(IsComObjectCreated(hoInfo))) Begin
        Send CreateComObject of hoInfo
    End
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoReader "qa_output/minutes_unprotected.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoInfo "encrypted" To bTemp1
    Showln "encrypted: " bTemp1
    Get ComLoadFile Of hoReader "qa_output/minutes_unprotected.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumPages Of hoReader To iTemp1
    Showln "pages: " iTemp1

    //  Sample output:

    //  encrypted, as loaded: True
    //  algorithm: rc4-128
    //  will be saved encrypted: False
    //  encrypted: False
    //  pages: 1


End_Procedure