IRONSOFTWAREHOME
PDF Encryption & Decryption
using IronPdf;
using System;

//Open an Encrypted File, alternatively create a new PDF from Html

var pdf = PdfDocument.FromFile("encrypted.pdf", "password");

//Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto";
pdf.MetaData.Keywords = "SEO, Friendly";
pdf.MetaData.ModifiedDate = DateTime.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 = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

// change or set the document encrpytion password
pdf.Password = "my-password";
pdf.SaveAs("secured.pdf");
Imports IronPdf
Imports System

'Open an Encrypted File, alternatively create a new PDF from Html

Dim pdf = PdfDocument.FromFile("encrypted.pdf", "password")

'Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = DateTime.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 = IronPdf.Security.PdfPrintSecurity.FullPrintRights

' change or set the document encrpytion password
pdf.Password = "my-password"
pdf.SaveAs("secured.pdf")
NuGet Download
Install-Package IronPdf
PDF Encryption & Decryption

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.

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, where the field-key is replaced with the name of the field you want to edit, such as Author, Keywords, modifiedDate, 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 numerous options allowing full customization of the security settings of your PDF, from disallowing user annotations to controlling the printing permission rights; this class ensures 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 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(). Learn to Set PDF Permissions and Passwords with IronPDF

View on GitHub

Ready to Get Started?

Nuget Downloads 20,667,543Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required