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 toFalseto 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.






