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 using 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
Package Manager Console
PM> Install-Package Aspose.Words
Protect Word Documents using 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:
- Create an instance of Document class and initialize it with the Word document’s path.
- Call Document.Protect(ProtectionType) method by providing desired ProtectionType value.
- Call Document.Save(String) method to save the protected 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:
- Create an instance of Document class and initialize it with the Word document’s path.
- Call Document.Protect(ProtectionType, String) method by providing the ProtectionType value and a password.
- Call Document.Save(String) method to save the protected Word document.
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:
- Create an instance of Document class and initialize it with the Word document’s path.
- Call Document.Unprotect() method.
- Call Document.Save(String) method to save the unprotected 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.