Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Protect a Document

  • 2 minutes to read

Document protection prevents users from modifying a document (add, format or delete document parts, insert tables, images, comments, etc.).

The following example demonstrates how to protect a document:

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();
  }
}

#Unprotect a Document

Use the Document.Unprotect method to remove protection.

View Example

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

  if (document.IsDocumentProtected == true)
  {
    // Unprotect the document
    document.Unprotect();

    // Insert a comment indicating that the document can be edited
    document.Comments.Create(document.Paragraphs[0].Range,"Admin");
    SubDocument commentDocument = document.Comments[0].BeginUpdate();
    commentDocument.InsertText(commentDocument.CreatePosition(0),
    "Document is unprotected. You can modify the document.");
    commentDocument.EndUpdate();
  }
}