How to make a Word document as Read-only document in C# and VB.NET?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can restrict a Word document from editing either by providing a password or without password.
The following are types of Microsoft Word document protection:
- AllowOnlyComments: You can add or modify only the comments in the Word document.
- AllowOnlyFormFields: You can modify the form field values in the Word document.
- AllowOnlyRevisions: Allow only revisions to be made to existing content. After enabling this flag, accept and reject changes options in Microsoft Word application are disabled.
- AllowOnlyReading: You can only view the content in the Word document.
- NoProtection: You can access or edit the Word document contents as normally.
From this page, you can learn how to make a Word document as Read-only document (prevent from editing) Syncfusion® Word library (Essential® DocIO) in C# and VB.NET.
Steps to make a Word document as Read-only document in C#
- Create a new C# console application project.
- Install Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS
- Use the following code to make a Word document as Read-only document.
C#
//Opens a Word document WordDocument document = new WordDocument(@"Template.docx"); //Sets the protection with password and it allows only to view the content in the Word document. document.Protect(ProtectionType.AllowOnlyReading, "password"); //Saves the Word document document.Save("Protection.docx", FormatType.Docx); document.Close();
VB
'Opens a Word document Dim document As New WordDocument("Template.docx") 'Sets the protection with password and it allows only to view the content in the Word document. document.Protect(ProtectionType.AllowOnlyReading, "password") 'Saves the Word document document.Save("Protection.docx", FormatType.Docx) document.Close()
A complete working example of making a Word document as Read-only document in C# can be downloaded from here.
By executing the program, you will get the output Word document as follows:
Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
An online example to protect the Word document from editing using DocIO..
See Also:
Protecting Word document from editing
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.