Protect Documents and Parts of Documents

You can add protection to Word documents to prevent users from making any edits to the document.

VSTO Words

The following code example protects the active document. To use this code example, run it from the ThisAddIn class in your project.

object noReset = false;
object password = System.String.Empty;
object useIRM = false;
object enforceStyleLock = false;
this.Application.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading,
ref noReset, ref password, ref useIRM, ref enforceStyleLock);

Aspose.Words

When a document is protected, the user can make only limited changes, such as adding annotations, making revisions, or completing a form. Even if a document is protected with a password, Aspose.Words does not require the password to open, modify or unprotect this document.

When you use Aspose.Words to protect a document, you have an option of keeping the existing password or specifying a new password.

Use the Document.Protect method to protect a document from changes. This method accepts a ProtectionType parameter and optionally a password by passing one as the second parameter Document.Protect.

Document doc = new Document();
doc.Protect(ProtectionType.AllowOnlyFormFields, "password");

Download Running Code Example

Sample Code