Protect PDF using Python

Set document privileges for PDF with AES-128 encryption. Use Aspose.PDF for Python for .NET to modify PDF documents programmatically

How to Protect PDF File Using Using Python for .NET Tool

In order to protect PDF file, use Aspose.PDF for Python via .NET, a powerful and easy-to-use API. Open PyPI, search for aspose-pdf, and install it. Alternatively, run the command:

Console

pip install aspose-pdf

Protect PDF using Python


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create an object of DocumentPrivilege & set options.
  3. Call Document.Encrypt method to protect PDF with AES-128.
  4. Save result in PDF format

Protect PDF - Python

This sample code shows how to protect PDF File

import aspose.pdf as apdf

from os import path
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)
document_privilege = apdf.facades.documentPrivilege.forbid_all
document_privilege.allow_screen_readers = True
document.encrypt("userpassword", "ownerpassword",
    document_privilege, apdf.CryptoAlgorithm.AE_SX128, False)
document.save(path_outfile)