Back to Devexpress

RichEditDocumentServer.EncryptedFilePasswordRequested Event

officefileapi-devexpress-dot-xtrarichedit-dot-richeditdocumentserver-67e71de9.md

latest8.1 KB
Original Source

RichEditDocumentServer.EncryptedFilePasswordRequested Event

Raises when the RichEditDocumentImportOptions.EncryptionPassword property is not set or returns an invalid password.

Namespace : DevExpress.XtraRichEdit

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

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public event EncryptedFilePasswordRequestedEventHandler EncryptedFilePasswordRequested
vb
Public Event EncryptedFilePasswordRequested As EncryptedFilePasswordRequestedEventHandler

Event Data

The EncryptedFilePasswordRequested event's data class is EncryptedFilePasswordRequestedEventArgs. The following properties provide information specific to this event:

PropertyDescription
CancelGets or sets a value indicating whether the event should be canceled. Inherited from CancelEventArgs.
DocumentNameGets the name of the encrypted document.
PasswordGets or sets the password used to encrypt the document.

Remarks

The EncryptedFilePasswordRequested event allows you to specify a password in code using the Password property. If the given password is empty or invalid, the RichEditDocumentServer.EncryptedFilePasswordCheckFailed event raises.

The code sample below shows how to handle the RichEditDocumentServer.EncryptedFilePasswordRequested and RichEditDocumentServer.EncryptedFilePasswordCheckFailed events to prompt users to enter a password. If the user cancels the operation or exceeds the number of attempts to enter the password, the RichEditDocumentServer loads an empty file.

View Example

csharp
private static void Server_EncryptedFilePasswordRequested(object sender, EncryptedFilePasswordRequestedEventArgs e)
{
    //Prompt the user to enter the password
    Console.WriteLine("Enter password:");
    e.Password = Console.ReadLine();
    e.Handled = true;
}

private static void Server_EncryptedFilePasswordCheckFailed(object sender, EncryptedFilePasswordCheckFailedEventArgs e)
{
    //Analyze the password error:
    switch (e.Error)
    {
        //If the password is empty, raise the request again
        case RichEditDecryptionError.PasswordRequired:
            Console.WriteLine("You did not enter the password!");
            e.TryAgain = true;
            e.Handled = true;
        break;
        //If the password is invalid, ask user whether to continue the operation:
        case RichEditDecryptionError.WrongPassword:
            Console.WriteLine("The password is incorrect. Try Again? (yes/no)");
            string answer = Console.ReadLine()?.ToLower();
            if (answer == "y")
            {
                e.TryAgain = true;
                e.Handled = true;
            }
            //If user cancels the operation, open an empty file:
            else
            {
                Console.WriteLine("Password check failed. Loading an empty file...");
            }
        break;
    }
}

private static void Server_InvalidFormatException(object sender, RichEditInvalidFormatExceptionEventArgs e)
{
    RichEditDocumentServer server = (RichEditDocumentServer)sender;

    //Save and open the resulting file
    server.SaveDocument("EmptyFile.docx", DocumentFormat.Docx);
    Process.Start("EmptyFile.docx");
}
vb
Private Shared Sub Server_EncryptedFilePasswordRequested(ByVal sender As Object, ByVal e As EncryptedFilePasswordRequestedEventArgs)
    'Prompt the user to enter the password
    Console.WriteLine("Enter password:")
    e.Password = Console.ReadLine()
    e.Handled = True
End Sub

Private Shared Sub Server_EncryptedFilePasswordCheckFailed(ByVal sender As Object, ByVal e As EncryptedFilePasswordCheckFailedEventArgs)

    'Analyze the password error:
    Select Case e.[Error]
        'If the password is empty, raise the request again
        Case RichEditDecryptionError.PasswordRequired
            Console.WriteLine("You did not enter the password!")
            e.TryAgain = True
            e.Handled = True
        'If the password is invalid, ask user whether to continue the operation:
        Case RichEditDecryptionError.WrongPassword
            Console.WriteLine("The password is incorrect. Try Again? (yes/no)")
            Dim answer As String = Console.ReadLine()?.ToLower()

            If answer = "y" Then
                e.TryAgain = True
                e.Handled = True
            'If user cancels the operation, open an empty file:
            Else
                Console.WriteLine("Password check failed. Loading an empty file...")
            End If
    End Select
End Sub

Private Shared Sub Server_InvalidFormatException(ByVal sender As Object, ByVal e As RichEditInvalidFormatExceptionEventArgs)
    'Save and open the resulting file
    Dim server As RichEditDocumentServer = CType(sender, RichEditDocumentServer)
    server.SaveDocument("EmptyFile.docx", DocumentFormat.Docx)
    Process.Start("EmptyFile.docx")
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the EncryptedFilePasswordRequested event.

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.

word-document-api-open-and-save-encrypted-files/CS/Program.cs#L19

csharp
RichEditDocumentServer server = new RichEditDocumentServer();
server.EncryptedFilePasswordRequested += Server_EncryptedFilePasswordRequested;
server.EncryptedFilePasswordCheckFailed += Server_EncryptedFilePasswordCheckFailed;

word-document-api-open-and-save-encrypted-files/VB/Program.vb#L14

vb
Dim server As RichEditDocumentServer = New RichEditDocumentServer()
AddHandler server.EncryptedFilePasswordRequested, AddressOf Server_EncryptedFilePasswordRequested
AddHandler server.EncryptedFilePasswordCheckFailed, AddressOf Server_EncryptedFilePasswordCheckFailed

Implements

EncryptedFilePasswordRequested

See Also

RichEditDocumentServer Class

RichEditDocumentServer Members

DevExpress.XtraRichEdit Namespace