Back to Devexpress

How to: Protect a PDF Document with a Password

officefileapi-114054-pdf-document-api-examples-document-protection-how-to-protect-a-pdf-document-with-a-password.md

latest3.9 KB
Original Source

How to: Protect a PDF Document with a Password

  • Sep 19, 2023
  • 2 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

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

Refer to the following topic for more information: Document Protection

View Example

csharp
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");

                // Initialize encryption options with owner and user passwords.
                PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions(
                    ownerPassword: "OwnerPassword",
                    userPassword: "UserPassword"
                );

                // Specify printing, data extraction, modification, and interactivity permissions.
                encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed;
                encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed;
                encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling;
                encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed;

                // 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 }
                );
            }
        }
    }
}
vb
Imports DevExpress.Pdf

Module PDFPasswordProtection
    Sub Main()
        Using pdfDocumentProcessor As New PdfDocumentProcessor()

            ' Load a PDF document.
            pdfDocumentProcessor.LoadDocument("..\..\Demo.pdf")

            ' Initialize encryption options with owner and user passwords.
            Dim encryptionOptions As New PdfEncryptionOptions(
                ownerPassword:="OwnerPassword",
                userPassword:="UserPassword"
            )

            ' Specify printing, data extraction, modification, and interactivity permissions.
            encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed
            encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed
            encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling
            encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed

            ' Specify the 256-bit AES encryption algorithm.
            encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256

            ' Save the protected document with encryption settings.
            pdfDocumentProcessor.SaveDocument(
                "..\..\ProtectedDocument.pdf",
                New PdfSaveOptions() With {.EncryptionOptions = encryptionOptions}
            )

        End Using
    End Sub
End Module

See Also

Get Started - Load and Edit a PDF File