Skip to main content
.NET Framework 4.5.2+

Document.Protect(String) Method

Protects a document with a given password.

Namespace: DevExpress.XtraRichEdit.API.Native

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

NuGet Package: DevExpress.RichEdit.Core

Declaration

void Protect(
    string password
)

Parameters

Name Type Description
password String

A string that specifies a password for the document. If an empty string is specified, the document can be unprotected without a password.

Remarks

The Protect method prevents end users from modifying the document. This method overload enables the DocumentProtectionType.ReadOnly editing mode.

The Document.IsDocumentProtected property indicates whether the document is protected.

The password specified as the method’s parameter is used to unlock the document in the UI using the Unprotect Document dialog.

To remove protection in code, call the Document.Unprotect method. It unlocks the document, ignoring the password.

The Protect method does not require user input. To allow end users to create a password to protect the document, execute the ProtectDocumentCommand command instead. Refer to the Restrictions and Protection topic for more details.

View Example: Protection - How to programmatically create a protected document and apply range permissions

Example

View Example

using (var wordProcessor = new RichEditDocumentSever())
{
  wordProcessor.LoadDocument("Documents//Grimm.docx");
  Document document = wordProcessor.Document;

  if (!document.IsDocumentProtected)
  {
    // Protect the document with a password
    document.Protect("123", DocumentProtectionType.ReadOnly);

    // Insert a comment indicating that the document is protected
    document.Comments.Create(document.Paragraphs[0].Range, "Admin");
    SubDocument commentDocument = document.Comments[0].BeginUpdate();
    commentDocument.InsertText(commentDocument.CreatePosition(0), 
    "Document is protected with a password.\nYou cannot modify the document until protection is removed.");
    commentDocument.EndUpdate();
  }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the Protect(String) method.

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.

See Also