Protect or Unprotect Word Documents Programmatically using C#

Microsoft Word supports a variety of protection features to protect the whole Word document or some parts of the document. Document protection comes into practice when you need to share the document with another person or party. In such cases, you may want to limit the third party’s access or permissions. On the other hand, you can also specify a password to avoid unauthorized access to the document. This article is also comprised of some easy ways to protect Word documents programmatically. Furthermore, you’ll also learn how to unprotect Word documents without a password. In the subsequent sections, you’ll learn how to:

  • protect Word documents with particular protection type in C#,
  • protect Word documents with a password in C#, and
  • unprotect Word documents without a password in C#.

.NET API to Protect/Unprotect Word Documents

Aspose.Words for .NET is an API to manipulate Word documents programmatically in the .NET applications. Along with other document manipulation features, the API provides easy yet powerful features to protect and unprotect Word documents. You can download Aspose.Words for .NET DLL or install it in your project using the following ways in Visual Studio:

NuGet Package Manager

protect or unprotect Word document

Package Manager Console

PM> Install-Package Aspose.Words

Protect Word Documents with a Protection Type in C#

Aspose.Words for .NET provides the following protection types to secure a Word document:

  • AllowOnlyComments – To allow modification of comments only.
  • AllowOnlyFormFields – To allow data entry into the form fields only.
  • AllowOnlyRevisions – To allow adding revision marks only.
  • ReadOnly – Completely read-only (no changes are allowed to the document).
  • NoProtection – No protection at all.

The following are the steps to apply protection to a Word document:

The following code sample shows how to protect a Word document in C#.

Protect Word Documents with Password in C#

The following are the steps to protect a Word document with a password:

The following code sample shows how to protect a Word document using a password in C#.

Unprotect Word Documents without a Password in C#

Aspose.Words for .NET has the ability to unprotect a Word document even if you don’t have the password. The following are the steps to unprotect a Word document:

The following code sample shows how to unprotect a Word document without a password in C#.

Try Aspose.Words for .NET for Free

You can get a temporary license to try and use Aspose.Words for .NET for free.