Back to Devexpress

How to: Protect a Document

officefileapi-118637-word-processing-document-api-examples-protection-how-to-protect-a-document.md

latest3.6 KB
Original Source

How to: Protect a Document

  • Sep 19, 2023
  • 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

csharp
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();
  }
}
vb
Using wordProcessor = New RichEditDocumentSever()
  wordProcessor.LoadDocument("Documents//Grimm.docx")
  Dim document As Document = wordProcessor.Document

  If Not document.IsDocumentProtected Then
    ' 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")
    Dim commentDocument As SubDocument = document.Comments(0).BeginUpdate()
    commentDocument.InsertText(commentDocument.CreatePosition(0), "Document is protected with a password." & ControlChars.Lf & "You cannot modify the document until protection is removed.")
    commentDocument.EndUpdate()
  End If
End Using

Unprotect a Document

Use the Document.Unprotect method to remove protection.

View Example

csharp
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();
  }
}
vb
Using wordProcessor = New RichEditDocumentSever()
  wordProcessor.LoadDocument("Documents//Grimm_Protected.docx")
  Dim document As Document = server.Document

  If document.IsDocumentProtected = True Then
    ' Unprotect the document
    document.Unprotect()

    ' Insert a comment indicating that the document can be edited
    document.Comments.Create(document.Paragraphs(0).Range,"Admin")
    Dim commentDocument As SubDocument = document.Comments(0).BeginUpdate()
    commentDocument.InsertText(commentDocument.CreatePosition(0), "Document is unprotected. You can modify the document.")
    commentDocument.EndUpdate()
  End If
End Using