DEV Community

IronSoftware
IronSoftware

Posted on

Set PDF Passwords and Permissions in C# (.NET Guide)

Our HR department emailed salary reports as PDFs. No password protection. Anyone with access to email could open them. A compliance audit flagged this as a critical security gap.

PDF password protection and permissions solved this. Here's the implementation.

How Do I Password-Protect a PDF?

Use OwnerPassword and UserPassword properties:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

var pdf = PdfDocument.FromFile("report.pdf");

pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.UserPassword = "user123";

pdf.SaveAs("protected.pdf");
Enter fullscreen mode Exit fullscreen mode

Users need user123 to open. admin123 grants full permissions.

What's the Difference Between User and Owner Passwords?

User Password: Required to open the PDF
Owner Password: Grants editing/printing permissions

Set both for maximum security:

pdf.SecuritySettings.OwnerPassword = "strong-admin-pwd";
pdf.SecuritySettings.UserPassword = "viewer-pwd";
Enter fullscreen mode Exit fullscreen mode

Can I Restrict Printing?

Yes. Use AllowUserPrinting:

var pdf = PdfDocument.FromFile("document.pdf");

pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.AllowUserPrinting = false;

pdf.SaveAs("no-print.pdf");
Enter fullscreen mode Exit fullscreen mode

Users can view but not print.

How Do I Make PDFs Read-Only?

Disable editing and annotations:

pdf.SecuritySettings.OwnerPassword = "admin123";
pdf.SecuritySettings.AllowUserEdits = false;
pdf.SecuritySettings.AllowUserAnnotations = false;

pdf.SaveAs("read-only.pdf");
Enter fullscreen mode Exit fullscreen mode

Can I Allow Form Filling But Not Editing?

Yes:

pdf.SecuritySettings.AllowUserFormData = true;
pdf.SecuritySettings.AllowUserEdits = false;
Enter fullscreen mode Exit fullscreen mode

Users fill forms but can't modify document structure.

How Do I Remove Password Protection?

Open with owner password and save without security:

var pdf = PdfDocument.FromFile("protected.pdf", "admin123");

pdf.SecuritySettings.OwnerPassword = null;
pdf.SecuritySettings.UserPassword = null;

pdf.SaveAs("unprotected.pdf");
Enter fullscreen mode Exit fullscreen mode

What Encryption Strength Is Used?

IronPDF uses 128-bit or 256-bit AES encryption. Specify in security settings:

pdf.SecuritySettings.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES256;
Enter fullscreen mode Exit fullscreen mode

256-bit is more secure but requires modern PDF readers.

How Do I Apply Security During Generation?

Set security before saving:

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential</h1>");

pdf.SecuritySettings.OwnerPassword = "admin";
pdf.SecuritySettings.UserPassword = "view";
pdf.SecuritySettings.AllowUserPrinting = false;

pdf.SaveAs("secure-report.pdf");
Enter fullscreen mode Exit fullscreen mode

Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.

Top comments (0)