Protect PDF via 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 Library

In order to protect PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Protect PDF via 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

    document = Document("template.pdf")
    # instantiate DocumentPrivileges object
    # apply restrictions on all privileges
    documentPrivilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll
    # allow screen reading
    documentPrivilege.AllowScreenReaders = True
    # encrypt the file with User and Owner password.
    document.Encrypt("userpassword", "ownerpassword", documentPrivilege, Aspose.Pdf.CryptoAlgorithm.AESx128, false);
    # save updated document
    document.Save("protected.pdf")