How to Password Protect a PDF and Unprotect it with Python [Quick & Simple]
PDFs are widely used for their stability and compatibility across platforms, making them ideal for file sharing, printing, and archiving. However, this popularity also brings up the challenge of ensuring PDF security. Adding permission or open passwords to PDFs is an effective way to protect sensitive content, but manually encrypting or decrypting files one by one can be effort-taking.
Luckily, in this article, we’ll show you how to password-protect a PDF file and unprotect it using Python, streamlining the process of managing PDF security. Let’s dive in!
Python Library to Password Protect and Unprotect PDFs
There are several Python APIs available for password-protecting and unprotecting PDF documents, including Spire.PDF, Aspose.PDF, and some online tools. Among these, Spire.PDF for Python (the complete name of Spire.PDF) stands out for its simplicity, rich functionality, and safety. In this guide, we will use Spire.PDF as an example to demonstrate the entire process. You can easily install it using the pip command:
pip install Spire.PDF
How to Use PdfSecurity.Encrypt to Customize PDF Security and Permissions
Before diving into the main topic, let's first examine the PdfSecurity.Encrypt() method, which is the key to encrypting and decrypting PDF documents. We’ll also explore its parameters in detail.
PdfSecurity.Encrypt(String openPassword, String permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize, String originalPermissionPassword) contains five parameters:
- openPassword
Specifies the password required to open the PDF document.
- permissionPassword
Sets the password needed to modify permissions or remove security settings.
- permissions
Defines the actions allowed on the PDF, such as printing or copying.
- keySize
Determines the encryption strength, such as 128-bit or 256-bit.
- originalPermissionPassword
Provides the existing permission password when updating security settings.
Values in PdfPermissionsFlags permissions:
Name | Description |
None | No permissions granted. |
Default | Default permissions, usually equivalent to allowing all basic actions. |
Allows the document to be printed. | |
EditContent | Allows editing the content of the document. |
CopyContent | Allows copying text or graphics from the document. |
EditAnnotations | Allows adding or modifying annotations. |
FillFields | Allows filling in form fields within the document. |
AccessibilityCopyContent | Allows content to be extracted for accessibility purposes, such as screen readers. |
AssembleDocument | Allows inserting, deleting, or rotating pages and assembling the document. |
FullQualityPrint | Allows printing at full quality (higher resolution). |
How to Password Protect a PDF Document Easily
Protecting a PDF file with a password using Python is a straightforward process. It involves three main steps: loading the PDF file, encrypting it with the desired password, and saving the secured document. Below, you'll find detailed instructions and a code example to guide you through the process seamlessly.
Steps to password-protect a PDF file:
Create a PdfDocument instance and load a PDF document with the PdfDocument.LoadFromFile() method.
Protect the PDF file with a password using the PdfDocument.Security.Encrypt() method.
Save the updated PDF with PdfDocument.SaveToFile() method.
Here is a code example of protecting a PDF file with an open password and a permission password while allowing others to print:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
doc = PdfDocument()
# Load a sample PDF file
doc.LoadFromFile("/Booklet.pdf")
# Encrypt the PDF file with an open password and a permission password
doc.Security.Encrypt("openPsd", "permissionPsd", PdfPermissionsFlags.Print, PdfEncryptionKeySize.Key128Bit)
# Save the result file
doc.SaveToFile("/Encrypted.pdf", FileFormat.PDF)
How to Unprotect a PDF File Quickly
When the information or data in a PDF is no longer confidential and can be accessed by others, it’s time to decrypt the document. This section will guide you through the process of unprotecting a password-protected PDF using Python, enabling easy access to the file for everyone.
Steps to unprotect a PDF file:
Create an object of the PdfDocument class, and read a password-protected PDF using the PdfDocument.LoadFromFile() method.
Unprotect the PDF by setting both the open password and permission password parameters to empty in the PdfDocument.Security.Encrypt() method.
Save the decrypted PDF with the PdfDocument.SaveToFile() method.
Here is the code example of unprotecting a PDF file:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
doc = PdfDocument()
# Load an encrypted PDF file
doc.LoadFromFile("/Encrypted.pdf", "openPsd")
# Set the open password and permission password as empty
doc.Security.Encrypt(str(), str(), PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permissionPsd")
# Save the result file
doc.SaveToFile("/RemovePassword.pdf", FileFormat.PDF)
The Conclusion
This page is all about how to password-protect a PDF and unprotect it. You can learn step-by-step instructions and code examples in this guide. Besides, the article offers an explanation of the key method for better understanding. We hope you find it useful!
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by