Back to Devexpress

PdfEncryptionOptions.UserPasswordString Property

officefileapi-devexpress-dot-pdf-dot-pdfencryptionoptions.md

latest7.8 KB
Original Source

PdfEncryptionOptions.UserPasswordString Property

Specifies a user password that is used to protect opening the document.

Namespace : DevExpress.Pdf

Assembly : DevExpress.Pdf.v25.2.Core.dll

NuGet Package : DevExpress.Pdf.Core

Declaration

csharp
public string UserPasswordString { get; set; }
vb
Public Property UserPasswordString As String

Property Value

TypeDescription
String

A String object that is a user password.

|

Property Paths

You can access this nested property as listed below:

Object TypePath to UserPasswordString
PdfSaveOptions

.EncryptionOptions .UserPasswordString

|

Remarks

To restrict the user from data extraction, data modification, interactive, or printing operations with a PDF document, create a PdfEncryptionOptions object using the PdfSaveOptions.EncryptionOptions property and specify corresponding permissions using the PdfEncryptionOptions.DataExtractionPermissions, PdfEncryptionOptions.ModificationPermissions,PdfEncryptionOptions.InteractivityPermissions, and PdfEncryptionOptions.PrintingPermissions properties.

To allow a user to have full access to a document, specify the owner password using the PdfEncryptionOptions.OwnerPasswordString property.

For more information, see the Document Protection topic.

Example

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the UserPasswordString property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

pdf-document-api-protect-document-with-password/CS/PDFPasswordProtection/Program.cs#L22

csharp
encryptionOptions.OwnerPasswordString = "OwnerPassword";
encryptionOptions.UserPasswordString = "UserPassword";

office-file-api-in-web-api-app/CS/Controllers/PdfController.cs#L77

csharp
PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions();
encryptionOptions.UserPasswordString = password;
encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256;

pdf-document-api-protect-document-with-password/VB/PDFPasswordProtection/Program.vb#L20

vb
encryptionOptions.OwnerPasswordString = "OwnerPassword"
encryptionOptions.UserPasswordString = "UserPassword"
' Specify the 256-bit AES encryption algorithm.

See Also

PdfEncryptionOptions Class

PdfEncryptionOptions Members

DevExpress.Pdf Namespace