Protect Word with Specified Protection Type in C#

Word Protection is a different from Word encryption. It allows users to open and view Word document but there are several permissions that users cannot edit or only can fill fields. Solution in this guide demonstrates solution to protect Word with specified protection type in C# and VB.NET with Spire.Doc for .NET. The following screenshot presents result after protecting with read-only.

Protect Word Document

Spire.Doc for .NET provides a method Document.Protect(ProtectionType type, String password) of Document class. There are five protection types offered.

No Protection: Sets document without protection.
AllowOnlyRevisions: Allows adding revision marks to Word.
AllowOnlyComments: Allows modifying comments in Word.
AllowOnlyFormFields: Allows entering data in form fields of Word.
AllowOnlyReading: Allows only reading Word.

The code below shows how to protect Word with AllowOnlyReading type. Download and install Spire.Doc for .NET and follow the code.

[C#]
using Spire.Doc;

namespace ProtectWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\.NET Framework.docx");
            //Protect Word
            document.Protect(ProtectionType.AllowOnlyReading, "123456");
            //Save and Launch
            document.SaveToFile("ProtectWord.docx");
            System.Diagnostics.Process.Start("ProtectWord.docx");        
        }
    }
}
[VB.NET]
Imports Spire.Doc

Namespace ProtectWord
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\.NET Framework.docx")
            'Protect Word
            document.Protect(ProtectionType.AllowOnlyReading, "123456")
            'Save and Launch
            document.SaveToFile("ProtectWord.docx")
            System.Diagnostics.Process.Start("ProtectWord.docx")
        End Sub
    End Class
End Namespace

Spire.Doc is a stand-alone component, enabling developers/programmers to generate, open, write, edit and save Word document in WPF, .NET and Silverlight without Microsoft Word installed on system.