Password protection involves encrypting the document to restrict unauthorized access. It typically includes two types of passwords: the user password (or open password), required to open the document, and the owner password (or permissions password), which controls permissions for editing, printing, and other actions.

IronPDF supports everything you need for Password and Permissions for your existing and new PDF files. Granular meta-data and security settings can be applied, this includes the ability to limit PDF documents to be unprintable, read-only, and encrypted; 128-bit encryption, decryption, and password protection are all supported.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Set a Password for a PDF

We have an example PDF file that we want to protect using IronPDF. Let's execute the following code to add a password to the PDF. In this example, we will use the password password123.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-add-password.cs
using IronPdf;

// Create an instance of ChromePdfRenderer to render HTML to PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a simple HTML string as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World");

// Set the owner's password to restrict editing permissions
pdf.SecuritySettings.OwnerPassword = "123password";

// Set the user's password to restrict opening the document
pdf.SecuritySettings.UserPassword = "password123";

// Save the protected PDF to a file named 'protected.pdf'
pdf.SaveAs("protected.pdf");
Imports IronPdf



' Create an instance of ChromePdfRenderer to render HTML to PDF

Private renderer As New ChromePdfRenderer()



' Render a simple HTML string as a PDF document

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World")



' Set the owner's password to restrict editing permissions

pdf.SecuritySettings.OwnerPassword = "123password"



' Set the user's password to restrict opening the document

pdf.SecuritySettings.UserPassword = "password123"



' Save the protected PDF to a file named 'protected.pdf'

pdf.SaveAs("protected.pdf")
$vbLabelText   $csharpLabel

The result is the following PDF which you can view by typing the password password123.

Open a PDF that has a Password

This section describes how to open a PDF that has a password. The PdfDocument.FromFile method has a second optional parameter which is the password. Provide the correct password to this parameter to open the PDF.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-open-password.cs
using IronPdf;

// Load a PDF document from a file using IronPdf, providing the path to the file and the password for decryption.
var pdf = PdfDocument.FromFile("protected.pdf", "password123");

// Perform your PDF-related tasks here, such as reading content, modifying, or extracting information.
// Example: You might want to access text or images within the PDF, or manipulate its pages.

// Save the modified PDF document to a new file.
// This creates a new PDF file "protected_2.pdf" with the modifications made.
pdf.SaveAs("protected_2.pdf");
Imports IronPdf



' Load a PDF document from a file using IronPdf, providing the path to the file and the password for decryption.

Private pdf = PdfDocument.FromFile("protected.pdf", "password123")



' Perform your PDF-related tasks here, such as reading content, modifying, or extracting information.

' Example: You might want to access text or images within the PDF, or manipulate its pages.



' Save the modified PDF document to a new file.

' This creates a new PDF file "protected_2.pdf" with the modifications made.

pdf.SaveAs("protected_2.pdf")
$vbLabelText   $csharpLabel

Advanced Security and Permissions Settings

The PdfDocument object also has MetaData fields you may set such as Author and ModifiedDate. You can also disable User Annotations, User Printing, and many more as shown below:

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-advanced.cs
using IronPdf;

// This code snippet demonstrates how to open an encrypted PDF file,
// modify its security settings to make it read-only, and then save it.

try
{
    // Open an encrypted PDF file with the specified password.
    var pdf = PdfDocument.FromFile("protected.pdf", "password123");

    // Edit the file security settings.
    // This ensures the PDF becomes read-only and certain user actions are restricted.

    // Remove existing passwords and encryption from the document.
    pdf.SecuritySettings.RemovePasswordsAndEncryption();

    // Make the PDF document read-only, using a specified key for additional security.
    pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");

    // Set user permissions:

    // Disallow adding annotations to the PDF.
    pdf.SecuritySettings.AllowUserAnnotations = false;

    // Disallow copy & paste of content from the PDF.
    pdf.SecuritySettings.AllowUserCopyPasteContent = false;

    // Disallow filling forms or submitting form data.
    pdf.SecuritySettings.AllowUserFormData = false;

    // Allow full rights for printing the PDF.
    pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

    // Save the modified PDF as a new file, with enhanced security settings.
    pdf.SaveAs("secured.pdf");

    // Inform the user of successful completion.
    Console.WriteLine("The PDF has been secured and saved as 'secured.pdf'.");
}
catch (Exception ex)
{
    // Handling exceptions that might occur due to file access or processing errors.
    Console.WriteLine("An error occurred: " + ex.Message);
}
Imports IronPdf



' This code snippet demonstrates how to open an encrypted PDF file,

' modify its security settings to make it read-only, and then save it.



Try

	' Open an encrypted PDF file with the specified password.

	Dim pdf = PdfDocument.FromFile("protected.pdf", "password123")



	' Edit the file security settings.

	' This ensures the PDF becomes read-only and certain user actions are restricted.



	' Remove existing passwords and encryption from the document.

	pdf.SecuritySettings.RemovePasswordsAndEncryption()



	' Make the PDF document read-only, using a specified key for additional security.

	pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")



	' Set user permissions:



	' Disallow adding annotations to the PDF.

	pdf.SecuritySettings.AllowUserAnnotations = False



	' Disallow copy & paste of content from the PDF.

	pdf.SecuritySettings.AllowUserCopyPasteContent = False



	' Disallow filling forms or submitting form data.

	pdf.SecuritySettings.AllowUserFormData = False



	' Allow full rights for printing the PDF.

	pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights



	' Save the modified PDF as a new file, with enhanced security settings.

	pdf.SaveAs("secured.pdf")



	' Inform the user of successful completion.

	Console.WriteLine("The PDF has been secured and saved as 'secured.pdf'.")

Catch ex As Exception

	' Handling exceptions that might occur due to file access or processing errors.

	Console.WriteLine("An error occurred: " & ex.Message)

End Try
$vbLabelText   $csharpLabel

The permissions setting is related to the document password and behaves as follows. For example, setting the AllowUserCopyPasteContent property to false is intended to prevent copy/paste of content:

  • No password set: Without a password, copy/paste of content remains blocked.
  • User password set: When a user password is set, entering the correct password will allow copy/paste of content.
  • Owner password set: When an owner password is set, entering only the user password will not unlock the copy/paste feature. However, entering the correct owner password will allow copy/paste of content.
Permissions window

A closely related article discusses predefined and custom metadata. Learn more by following this link: "How to Set and Edit PDF Metadata."

Frequently Asked Questions

What are the two types of passwords used in PDF protection?

IronPDF uses two types of passwords for PDF protection: the user password (or open password), which is required to open the document, and the owner password (or permissions password), which controls permissions for editing, printing, and other actions.

How do you set a password for a PDF?

To set a password for a PDF using IronPDF, you need to load the existing PDF document with PdfDocument.FromFile, then set the OwnerPassword and UserPassword properties, and finally save the document with the specified passwords.

What level of encryption is supported for securing PDFs?

IronPDF supports 128-bit encryption for securing PDFs, providing a robust level of protection for sensitive documents.

Can permissions like printing and annotations be controlled?

Yes, IronPDF can control permissions such as printing, annotations, and other user actions by setting properties like AllowUserAnnotations and AllowUserPrinting in the SecuritySettings of the PdfDocument.

How can you open a password-protected PDF?

To open a password-protected PDF with IronPDF, use the PdfDocument.FromFile method and provide the correct password as the second parameter to access the document.

What additional features are offered for PDF security?

IronPDF offers features like setting document metadata (e.g., Author, ModifiedDate) and controlling user permissions (e.g., annotations, printing) to enhance PDF security.

How does setting an owner password affect user permissions?

Setting an owner password allows control over user permissions. For example, even if a user password is set, entering only the user password will not unlock certain features, like copy/paste, unless the correct owner password is also provided.

Is there a cost-effective alternative to yearly PDF security subscriptions?

IronSecureDoc offers a cost-effective solution for managing digital signing, redaction, encryption, and protection, all for a one-time payment, as an alternative to yearly PDF security subscriptions.

How can you further customize a PDF?

In addition to security settings, IronPDF allows customization of PDF files by setting metadata fields like Author and ModifiedDate and applying advanced security settings to control user interactions.

Where can I find more documentation on IronSecureDoc?

More documentation on IronSecureDoc can be explored at IronSoftware's documentation page, providing detailed information on managing SaaS services and PDF security features.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.