Let’s discuss how we can restrict access to Word documents by making them password protected. We have already learned to lock and unlock PDF, and PowerPoint files. In this article, we will see how to password protect a Word document using C#. Further, we will learn to remove the password to unlock Word documents, and lastly, how to change the existing password of DOC & DOCX files within the .NET applications.
The following topics are discussed below:
- .NET API to Password Protect Word Documents
- Add Password to Word Document
- Change Password of Word Document
- How to Remove Password from Word Document
.NET API to Password Protect Word Documents
GroupDocs.Merger provides the .NET API that allows locking and unlocking of Word documents within .NET applications. We will use GroupDocs.Merger for .NET to add, change, and remove password protection. In addition to protecting and unprotecting Word documents, there is much more that can be done with Word documents using the API. Documentation is available that explains detailed features, supported file formats, and much more.
PM> Install-Package GroupDocs.Merger
Password Protect Word Document in C#
Let’s discuss how to add a password to word documents and make them password protected. The following steps show how to lock a word document (DOC/DOCX) with a password using C#.
- Set the password options using the AddPasswordOptions.
- Load the document using the Merger class.
- Add the password to lock the loaded Word document using AddPassword method.
- Save the password protected file using the Save method.
The following code snippet shows how to password protect a Word document using C#.
Now, when you try to open the password-protected document, the document viewer & editor will ask for the password to open the file.
Change Existing Password of Word Document in C#
Your old password might have been too common that has been guessed. Let’s change it and be more careful next time. The following steps guide how to change the existing password of Word document using C#.
- Prepare the LoadOptions using the current password.
- Define the UpdatePasswordOptions using the new password.
- Load the DOC/DOCX file using the Merger class.
- Change the password using the UpdatePassword method.
- Save the protected document having new password using Save method.
Here is the C# code snippet that changes the existing password of a DOCX file.
Remove Password from Word Document in C#
Now let’s remove the protection from the documents that are no more confidential. First, open the Word document and then remove the password to make it unlocked. The following steps show how to unlock the Word document by removing the password using C#.
- Use the document’s existing password to prepare LoadOptions.
- Load the Word document using Merger class.
- Remove its password using RemovePassword method.
- Save the unlocked file in DOC/DOCX format by calling the Save method.
The following code sample unlocks the Word document of DOCX format by removing its password using C#
Let’s summarize what we learned today. Using a simple Word document, first, we made it password protected using C#. Next, we learned to change the existing password of a Word document. Lastly, we learned how to remove the password from the Word file to make it unlocked within any .NET application.
To learn more about GroupDocs.Merger for .NET, visit its documentation to start building your own document protector or password remover applications for various supported document formats. For queries, contact us via the forum.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.