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.

Password Protect Word Documents using C#

The following topics are discussed below:

.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.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Merger

Password Protect Word Document in C#

Programmatically Locked Word Doc

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.

Enter Password to Open Protected Word Document

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#.

Here is the C# code snippet that changes the existing password of a DOCX file.

Remove Password from Word Document in C#

Programmatically unlocked Word Document

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#

Conclusion

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.

See Also