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

Remove the Restrictions from a PDF

See more PDF Protection Examples

Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.

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 vOpts
    Handle hoOpts
    Variant vExtractOut
    Handle hoExtractOut
    Variant vSb
    Handle hoSb
    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 no_copy_permission.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End

    //  This PDF opens without a password, but copying text from it is not permitted.
    //  Removing the restrictions needs owner rights: load with the owner password.
    Set ComOwnerPassword Of hoPdf To "owner-secret"
    Get ComLoadFile Of hoPdf "qa_data/pdf/no_copy_permission.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.permissions.copy" To bTemp1
    Showln "may copy, before: " bTemp1
    Get ComBoolOf Of hoJsonOut "loaded.userPasswordRequired" To bTemp1
    Showln "password needed to open: " bTemp1

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

    //  With no user password and nothing restricted there is nothing left to protect, so the PDF
    //  will be saved unencrypted.  A PDF that needs a password to open would stay encrypted and
    //  keep its password.
    Get ComBoolOf Of hoJsonOut "pending.encrypted" To bTemp1
    Showln "will be saved encrypted: " bTemp1

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

    //  Anyone can now extract the text.
    Get Create (RefClass(cComChilkatPdf)) To hoReader
    If (Not(IsComObjectCreated(hoReader))) Begin
        Send CreateComObject of hoReader
    End
    Get ComLoadFile Of hoReader "qa_output/leaflet_unrestricted.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoOpts
    If (Not(IsComObjectCreated(hoOpts))) Begin
        Send CreateComObject of hoOpts
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoExtractOut
    If (Not(IsComObjectCreated(hoExtractOut))) Begin
        Send CreateComObject of hoExtractOut
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoOpts to vOpts
    Get pvComObject of hoSb to vSb
    Get pvComObject of hoExtractOut to vExtractOut
    Get ComExtractTextSb Of hoReader "text" vOpts vSb vExtractOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLength Of hoSb To iTemp1
    Showln "characters extracted: " iTemp1

    //  Sample output:

    //  may copy, before: False
    //  password needed to open: False
    //  will be saved encrypted: False
    //  characters extracted: 216


End_Procedure