Protection in PDF Documents
- 5 minutes to read
By default, most documents do not impose restrictions on opening a file used for reading. The PDF Document API component can protect a document with a password using both user and owner passwords. Passwords are used to prevent users from accessing or modifying PDF documents.
The sections below provide more detailed descriptions on the user and owner passwords and explain how to customize PDF Document API encryption options to protect a document.
Encryption Settings
Before encrypting, the PDF Document API component must have a document (the document can be created by any operation using, for example, the Document Creation API).
All required settings to protect a document are contained in the PdfEncryptionOptions object, which you can access using the PdfSaveOptions.EncryptionOptions property.
To encrypt the document with these settings, call the overloaded PdfDocumentProcessor.CreateEmptyDocument or PdfDocumentProcessor.SaveDocument method and pass the PdfSaveOptions object containing these settings as a parameter.
User password
The user password is used when you want to prevent unauthorized access to a document. This password protects sensitive information. So, if you want to open a protected document, you need to enter the user password.
The image below shows a dialog box displayed if you are trying to open a protected document in the PDF Viewer.
Specify the user password using the PdfEncryptionOptions.UserPasswordString property to protect your document from being opened.
Owner password
If a document is opened with a user password or without a password (if the user password is not specified), you can perform only permitted operations. To get full access to a document, specify the owner password using the PdfEncryptionOptions.OwnerPasswordString property.
Important
If the Owner and User passwords are the same or the Owner password is missing and the document is protected only with the User password, the resulting document will have a document opening restriction. In this case, any user will have full access to this document after opening it.
You can control the document access either by restricting or allowing printing, data extraction, document modification and interactive operations.
Note
The restrictions on data extraction, data modification, interactive, or printing operations with a document can’t be applied without the owner password.
How to Restrict Operations
To restrict data extraction, data modification, interactive, or printing operations with a PDF document, create a PdfEncryptionOptions object and specify the owner password using the PdfEncryptionOptions.OwnerPasswordString property and permissions listed below.
To encrypt a document with printing permissions, use the PdfEncryptionOptions.PrintingPermissions property. This property can be set to one of the following values.
Name | Description |
---|---|
Allowed | Permit document printing. |
LowQuality | Prohibit document printing at the highest quality level. |
NotAllowed | Prohibit document printing. |
To encrypt a document with data extraction permissions, use the PdfEncryptionOptions.DataExtractionPermissions property. These permissions impose the following restrictions on data extraction operations.
Name | Description |
---|---|
Allowed | Permit data extraction operations (copying, or text and graphics extraction from the document) including access to software that uses assistive technologies. |
Accessibility | Allow PDF Viewers to access document content by using the Viewer’s accessibility features. |
NotAllowed | Prohibit data extraction operations (copying, or text and graphics extraction from the document) including access to software that uses assistive technologies. |
To encrypt a document with document modification restrictions, set the PdfEncryptionOptions.ModificationPermissions property to one of the following values.
Name | Description |
---|---|
Allowed | Permit document modification and assembling. |
DocumentAssembling | Allow only document assembling such as inserting, rotating or deleting pages, as well as bookmark creation on the navigation pane. |
NotAllowed | Prohibit document modification and assembling. |
To set interactivity permissions, use the PdfEncryptionOptions.InteractivityPermissions property. The following values are supported.
Name | Description |
---|---|
Allowed | Permit interactive operations (adding or modifying text annotations, filling in interactive form fields, and creating or modifying interactive form fields) in a PDF document. |
FormFillingAndSigning | Prohibit interactive operations in a PDF document except form filling in the existing form fields and document signing. |
NotAllowed | Prohibit all interactive operations (adding or modifying text annotations, filling in interactive form fields, and creating or modifying interactive form fields) in a PDF document. |
Example
This example shows how to protect a PDF document using both the owner and user passwords.
using DevExpress.Pdf;
namespace PDFPasswordProtection {
class Program {
static void Main(string[] args) {
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) {
// Load a PDF document.
pdfDocumentProcessor.LoadDocument("..\\..\\Demo.pdf");
// Specify printing, data extraction, modification, and interactivity permissions.
PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions();
encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed;
encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed;
encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling;
encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed;
// Specify the owner and user passwords for the document.
encryptionOptions.OwnerPasswordString = "OwnerPassword";
encryptionOptions.UserPasswordString = "UserPassword";
// Specify the 256-bit AES encryption algorithm.
encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256;
// Save the protected document with encryption settings.
pdfDocumentProcessor.SaveDocument("..\\..\\ProtectedDocument.pdf", new PdfSaveOptions() { EncryptionOptions = encryptionOptions });
}
}
}
}