PDF Encryption & Decryption
from ironpdf import *

# Open an Encrypted File, alternatively create a new PDF from Html
pdf = PdfDocument.FromFile("encrypted.pdf", "password")
 
# Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = Now()
 
# Edit file security settings
# The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserFormData = False
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
 
# Change or set the document encryption password
pdf.Password = "my-password"
pdf.SaveAs("secured.pdf")
pip install ironpdf
PDF Encryption & Decryption

PDF Encryption & Decryption

IronPDF for Python can open password-protected PDF files, modify their metadata and security settings, and re-save them with a new password - enabling full encryption and decryption workflows in a few lines of code.

Getting Started

Pass the file path and existing password to PdfDocument.FromFile to open an encrypted PDF. Modify security settings and metadata as needed, then assign a new password to pdf.Password and save with SaveAs.

Understanding the Code

  • PdfDocument.FromFile(path, password): Opens an encrypted PDF file using the provided password for decryption. Raises an exception if the password is incorrect.
  • MetaData.Author / Keywords / ModifiedDate: Metadata fields that can be read or updated after decryption.
  • SecuritySettings.RemovePasswordsAndEncryption(): Strips the current encryption from the in-memory document, allowing security settings to be reconfigured.
  • SecuritySettings.MakePdfDocumentReadOnly(key): Re-applies read-only protection with a new internal key.
  • SecuritySettings.AllowUser*: Fine-grained user permission flags - set to False to restrict specific actions.
  • pdf.Password = "my-password": Sets a new password that will be required to open the saved PDF.
  • SaveAs("secured.pdf"): Saves the re-encrypted document with all new settings applied.

Decryption Without Re-Encryption

To produce an unencrypted copy of a password-protected PDF, call RemovePasswordsAndEncryption() and save without setting a new Password.

Learn to secure PDF documents with IronPDF for Python!

Ready to Get Started?

Version:2026.11just released