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

Restrict Printing, Copying and Editing of a PDF

See more PDF Protection Examples

Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.

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 vEmpty
    Handle hoEmpty
    Variant vInfo
    Handle hoInfo
    Variant vOpts
    Handle hoOpts
    Variant vExtractOut
    Handle hoExtractOut
    Variant vSb
    Handle hoSb
    Handle hoOwner
Info    Handle hoOwnerInfo
    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 two_page_memo.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  No user password: the PDF opens without a prompt.  The owner password is what protects the
    //  restrictions, so it must not be empty when anything is restricted.
    Set ComUserPassword Of hoPdf To ""
    Set ComOwnerPassword Of hoPdf To "owner-secret"

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "permissions.print" "lowQuality" To iSuccess
    Get ComUpdateBool Of hoJson "permissions.copy" False To iSuccess
    Get ComUpdateBool Of hoJson "permissions.modify" False To iSuccess
    Get ComUpdateBool Of hoJson "permissions.annotate" False To iSuccess
    //  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

    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 "encrypt" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    //  Anyone can open the result, with the rights of a user.
    Get Create (RefClass(cComChilkatPdf)) To hoReader
    If (Not(IsComObjectCreated(hoReader))) Begin
        Send CreateComObject of hoReader
    End
    Get ComLoadFile Of hoReader "qa_output/memo_restricted.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoEmpty
    If (Not(IsComObjectCreated(hoEmpty))) Begin
        Send CreateComObject of hoEmpty
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
    If (Not(IsComObjectCreated(hoInfo))) Begin
        Send CreateComObject of hoInfo
    End
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoInfo to vInfo
    Get ComProtection Of hoReader "getInfo" vEmpty vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoInfo "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1
    Get ComStringOf Of hoInfo "loaded.permissions.print" To sTemp1
    Showln "print: " sTemp1
    Get ComBoolOf Of hoInfo "loaded.permissions.copy" To bTemp1
    Showln "copy: " bTemp1
    Get ComBoolOf Of hoInfo "loaded.permissions.modify" To bTemp1
    Showln "modify: " bTemp1

    //  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
    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
        Showln "Text extraction was refused: the PDF does not permit copying."
    End

    //  The owner password lifts every restriction.
    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_output/memo_restricted.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoOwnerInfo
    If (Not(IsComObjectCreated(hoOwnerInfo))) Begin
        Send CreateComObject of hoOwnerInfo
    End
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoOwnerInfo to vOwnerInfo
    Get ComProtection Of hoOwner "getInfo" vEmpty vOwnerInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoOwnerInfo "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1
    Get pvComObject of hoOpts to vOpts
    Get pvComObject of hoSb to vSb
    Get pvComObject of hoExtractOut to vExtractOut
    Get ComExtractTextSb Of hoOwner "text" vOpts vSb vExtractOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoOwner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLength Of hoSb To iTemp1
    Showln "characters extracted with the owner password: " iTemp1

    //  Sample output:

    //  opened with: emptyPassword
    //  print: lowQuality
    //  copy: False
    //  modify: False
    //  Text extraction was refused: the PDF does not permit copying.
    //  opened with: owner
    //  characters extracted with the owner password: 1048


End_Procedure