PDF Encryption & Decryption
PDF security is an important aspect of working with PDF files, and IronPDF takes PDF security seriously. With IronPDF, you can encrypt and decrypt PDF files by applying custom metadata and security settings to your PDF. IronPDF supports 128-bit encryption on your encrypted files, the ability to decrypt previously encrypted files (given you have the correct passwords needed to access the file), and apply password protection to your existing or newly created PDF documents.
Steps to Convert URL to PDF in C#
var pdf = PdfDocument.FromFile("sample.pdf", "password");
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.Password = "my-password";
The first step to encrypting and decrypting your PDF files using IronPDF is to either create a new PDF file, or load an existing one as we have here. While loading our PDF file using PdfDocument.FromFile
we have passed two parameters to the FromFile
method, the first is the PDF's file path and the second is the password to open the encrypted PDF.
The next lines are editing the metadata for our encrypted PDF. pdf.MetaData.Author
, for example, is used to edit the Author field with our custom string value. Each metadata field can be accessed using pdf.metadata.field-key
wherein the field-key is replaced with the name of the field you're wanted to edit, such as Author, Keywords, modifiedData, etc. This is especially helpful for giving your PDF keywords which make it easier to find, a customized modified date, a new author, or anything else you require to be customized within the MetaData properties.
The next line removes any current passwords and encryptions from the PDF document. This decryption process allows you to save the PDF without any encryption on it, or clears it for you to add new encryption settings for the PDF, which is what we'll do next. pdf.SecuritySettings.setting
gives you the ability to customize the security settings for your PDF, where setting
is replaced by the actual setting you wish to change, for example, pdf.SecuritySettings.MakePdfDocumentReadOnly
sets the PDF to be read-only. The PdfSecuritySettings class contains plenty of options allowing for full customization options for the security settings of your PDF, from disallowing user annotations, to controlling the printing permission rights, this class ensure proper PDF security is handled securely and efficiently.
The pdf.Password
is used to change the password for your PDF, or set a new one if there isn't already an existing one. This password encrypts the PDF file using a strong 128-bit encryption, protecting it against unauthorized access. Once you are happy with the metadata and security settings, you can save the PDF document to the desired file location using SaveAs()
.
Click here to view the How-to Guide, including examples, sample code, and files >