Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.1
  • Protect and Encrypt PDF Documents Using the New DevExpress PDF Document API

    • 4 minutes to read

    The DevExpress PDF Document API can protect and encrypt documents. It can also work with documents that are already protected or encrypted.

    Open Encrypted PDF Documents

    Pass the password as the PdfDocument constructor parameter to open encrypted PDF documents.

    When you open an encrypted PDF document, you can check permissions and access mode (user type). Use the following options to obtain this information:

    AccessMode
    Indicates the level of access granted to the user.
    DataExtractionPermissions
    Indicates whether the user is allowed to extract content from the document.
    InteractivityPermissions
    Indicates whether the user is allowed to interact with the document.
    ModificationPermissions
    Indicates whether the user is allowed to modify the document.
    PrintPermissions
    Indicates whether the user is allowed to print the document.

    The following example opens an encrypted PDF document and generates a document permissions report.

    Encryption Console Report

    using DevExpress.Docs.Pdf;
    using System;
    using System.IO;
    
    using (FileStream fileStream = File.OpenRead(@"C:\Test Documents\Document.pdf"))
    {
        using (PdfDocument pdfDocument = new PdfDocument(fileStream, new LoadOptions { Password = "your_password_here" }))
        {
            Console.WriteLine(
                String.Format(
                    $"Access mode: {pdfDocument.AccessMode}; " +
                    $"\r\n Data extraction permissions: {pdfDocument.DataExtractionPermissions} " +
                    $"\r\n Modification permissions: {pdfDocument.ModificationPermissions}" +
                    $"\r\n Interactivity permissions: {pdfDocument.InteractivityPermissions}"));
            Console.ReadKey();
        }
    }
    

    The PdfDocument class stores the document’s permissions and encryption settings when you call the PdfDocument.Save method. Call the PdfDocument.RemoveEncryption() method to remove encryption from the document.

    Set Permissions and Encrypt PDF Documents

    Create an EncryptionOptions object to specify encryption settings for a PDF document. You can choose from different encryption algorithms and set access permissions.

    Pass this EncryptionOptions object to the PdfDocument.Encrypt method to encrypt the document.

    User and Owner Passwords

    The UserPassword parameter of the EncryptionOptions constructor sets the password for the file. Use this password to protect sensitive information by preventing unauthorized access to the document.

    The OwnerPassword parameter of the EncryptionOptions constructor specifies the permission (or owner) password. A user can open the file without this password. This password restricts certain operations in the PDF document.

    Note

    If the Owner and User passwords are the same or the Owner password is missing and you protect the document only with the User password, the resulting document only has a document opening restriction. In this case, any user has full access to this document after opening it.

    Restrict Document Operations

    Use the following options to restrict data extraction, data modification, interactive, or printing operations:

    Example: Set Permissions and Encrypt a PDF Document with the User and Owner Passwords

    The following example sets permissions and encrypts a PDF document with the user and owner passwords.

    using DevExpress.Docs.Pdf;
    using System.IO;
    
    using (FileStream fileStream = File.OpenRead(@"C:\Test Documents\Document.pdf"))
    {
        using (PdfDocument pdfDocument = new PdfDocument(fileStream, new LoadOptions { Password = "your_password_here" }))
        {
            var encryptionOptions = new EncryptionOptions(
                ownerPassword: "ownerPassword",
                userPassword: "userPassword"
            );
    
            encryptionOptions.DataExtractionPermissions =
                DocumentDataExtractionPermissions.NotAllowed;
            encryptionOptions.PrintPermissions =
                DocumentPrintPermissions.LowQuality;
            encryptionOptions.ModificationPermissions =
                DocumentModificationPermissions.NotAllowed;
            encryptionOptions.Algorithm = EncryptionAlgorithm.AES256;
    
            pdfDocument.Encrypt(encryptionOptions);
            pdfDocument.Save(
                new FileStream(@"C:\Test Documents\Document_upd.pdf", FileMode.Create, FileAccess.Write));
        }
    }