Skip to main content
A newer version of this page is available. .

How to: Protect a PDF Document with a Password

  • 2 minutes to read

Important

You require a license to the DevExpress Office File API or DevExpress Universal Subscription to use these examples in production code. Refer to the DevExpress Subscription page for pricing information.

This example shows how a PDF document can be protected using both the owner and user passwords.

For more information, see the Document Protection topic.

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 });
            }
        }
    }
}
See Also