C#/VB.NET: Protect or Unprotect a Word Document

Word documents can be protected in a variety of ways, depending on the security requirements. To prevent unauthorized people from opening a document, you can encrypt it with a password. To allow users to open the document, but not edit or modify its content, you can make the document read-only or mark it as final. To allow users to modify parts of the document, you can lock the entire document but let specified sections available for editing. This article focuses on how to protect or unprotect a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Password Protect a Word Document in C#, VB.NET

Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to protect a Word document with a password using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Encrypt the document with a password using Document.Encrypt() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace PasswordProtectWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load a Word file
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\test.docx");

            //Encrypt the document with a password
            document.Encrypt("open-psd");

            //Save the document to another Word file
            document.SaveToFile("Encryption.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Change Permission of a Word Document in C#, VB.NET

Documents encrypted with an open password cannot be opened by those who do not know the password. If you’d like to grant people permission to read your document but restrict the types of modifications that someone can make, you can set the document permission. The following are the steps to change permission of a Word document using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Set the document permission and set the permission password using Document.Protect() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ChangeDocumentPermission
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document(); 

            //Load a Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Set the document permission and set the permission password
            document.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd");

            //Save the document to another Word file
            document.SaveToFile("Permission.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Lock Specified Sections of a Word Document in C#, VB.NET

When you protect your document, you can lock parts of it so that they cannot be changed and leave the unlocked parts available for editing. The following are the steps to protect specified sections of a Word document using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Set the editing restriction as AllowOnlyFormFields.
  • Unprotect a specific section by setting Document.Sections[index].ProtectForm to false. The rest sections will continue to be protected.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ProtectSpecificSection
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Set editing restriction as "AllowOnlyFormFields"
            doc.Protect(ProtectionType.AllowOnlyFormFields, "permissionPsd");

            //Unprotect section 2
            doc.Sections[1].ProtectForm = false;

            //Save the document to another Word file
            doc.SaveToFile("ProtectSection.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Mark a Word Document as Final in C#, VB.NET

By marking a document as Final, you disable typing, editing, and format changes capabilities and a message will appear to any reader that the document has been finalized. The following are the steps to mark a Word document as final using Spire.Doc for .NET.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Get the CustomDocumentProperties object from the document.
  • Add a custom property "_MarkAsFinal" to the document.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace MarkAsFinal
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //Get custom document properties 
            CustomDocumentProperties customProperties = doc.CustomDocumentProperties;

            //Add "_MarkAsFinal" property to the document
            customProperties.Add("_MarkAsFinal", true);

            //Save the document to another Word file
            doc.SaveToFile("MarkAsFinal.docx");
        }
    }
}

C#/VB.NET - How to Protect or Unprotect a Word Document

Remove Password from a Word Document in C#, VB.NET

You can remove the password from an encrypted document if the encryption is no longer needed. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Remove the password using Document.RemoveEncryption() method.
  • Save the document to another Word file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace RemovePassword
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load an encrypted Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Encryption.docx", FileFormat.Docx, "open-psd");

            //Remove encryption
            document.RemoveEncryption();

            //Save the document to another Word file
            document.SaveToFile("Decryption.docx", FileFormat.Docx);
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.