If your Word document contains sensitive or confidential information that you don’t want anyone else to access, protect it with a password is a good idea. This article demonstrates how to protect a Word document with a password in C# and VB.NET. Moreover, it also covers how to make a Word document ready only along with how to allow certain types of editing in a Word document.
Installation
This article uses Free Spire.Doc for .NET library to protect Word documents. The DLL files of the library can be either downloaded from the official website or installed via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then execute the following command:
PM> Install-Package FreeSpire.Doc
Protect Word Document with Password
You can follow the steps below protect a Word document with password:
- Create an instance of Document class.
- Load the Word document using Document.LoadFromFile() method.
- Protect the document with your password using Document.Encrypt() method.
- Save the result document using Document.SaveToFile() method.
C#
using Spire.Doc;
namespace ProtectWordDocumentWithPassword
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");
//Protect the document with your password
document.Encrypt("password");
//Save the result document
document.SaveToFile("PasswordProtected.docx", FileFormat.Docx);
}
}
}
VB.NET
Imports Spire.Doc
Namespace ProtectWordDocumentWithPassword
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")
'Protect the document with your password
document.Encrypt("password")
'Save the result document
document.SaveToFile("PasswordProtected.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2022/03/passwordprotectword.png?w=419)
Make a Word Document Read Only
If you do not want recipients to accidentally change your document, you can make the document read only by using the Document.Protect(ProtectionType.AllowOnlyReading, string) method.
The following are the steps to do so:
- Create an instance of Document class.
- Load the Word document using Document.LoadFromFile() method.
- Make the document read only using Document.Protect(ProtectionType.AllowOnlyReading, string) method.
- Save the result document using Document.SaveToFile() method.
C#
using Spire.Doc;
namespace MakeWordDocumentReadOnly
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");
//Make the document read only
document.Protect(ProtectionType.AllowOnlyReading, "password");
//Save the result document
document.SaveToFile("ReadOnly.docx");
}
}
}
VB.NET:
Imports Spire.Doc
Namespace MakeWordDocumentReadOnly
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")
'Make the document read only
document.Protect(ProtectionType.AllowOnlyReading, "password")
'Save the result document
document.SaveToFile("ReadOnly.docx")
End Sub
End Class
End Namespace
Allow Certain Types of Editing in a Word Document
You can control the types of changes others can make to your document, such as filling in forms only or adding comments only, by using the Document.Protect(ProtectionType, string) method.
Here are the protection types you can set:
- ProtectionType.AllowOnlyFormFields: allowing filling in forms only
- ProtectionType.AllowOnlyComments: allowing adding comments only
- ProtectionType.AllowOnlyRevisions: allowing revisions only
The following code example shows how to allow adding comments only in a Word document:
C#
using Spire.Doc;
namespace RestrictEditingType
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a Word document
document.LoadFromFile("Sample.docx");
//Allow adding comments only
document.Protect(ProtectionType.AllowOnlyComments, "password");
//Save the result document
document.SaveToFile("CommentsOnly.docx");
}
}
}
VB.NET
Imports Spire.Doc
Namespace RestrictEditingType
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New Document()
'Load a Word document
document.LoadFromFile("Sample.docx")
'Allow adding comments only
document.Protect(ProtectionType.AllowOnlyComments, "password")
'Save the result document
document.SaveToFile("CommentsOnly.docx")
End Sub
End Class
End Namespace
See More
Product Page | Documentation | Examples | Forum |