How to Encrypt Word Document – C#/VB.NET

Encrypt Word is the most frequently used method to protect document.

Actually, there are several important and secret documents in our daily life, such as technology materials of one company, papers and so on. In order to protect these documents, owners will encrypt them with passwords. Just having the correct passwords, people can open encrypted documents.

Besides encryption with passwords, owner can set permissions for document. For example, I can allow that one document can be edited or printed.

In this post, I will introduce a simple method to encrypt Word document with C#/VB.NET.

Note:Spire.Doc dll file has been added as reference in project.

Detailed Steps Shown as Following:

  1. Load document which I want to encrypt from computer.
  2. Encrypt document by using document.Encrypt() method. There is a parameter passed to method, string password.
  3. Save and launch file.

Main Coding:

C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Encrypt_Word
{
    class Encryption
    {
        static void Main(string[] args)
        {

            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\My Documents\Essays.docx”);

            //Encrypt
            document.Encrypt(“1234567”);

            //Save and Launch
            document.SaveToFile(“Encryption.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Encryption.docx”);
        }
    }
}

VB.NET

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Encrypt_Word
    Friend Class Encryption
        Shared Sub Main(ByVal args() As String)

            ‘Load Document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\My Documents\Essays.docx”)

            ‘Encrypt
            document.Encrypt(“1234567”)
            ‘Save and Launch

            document.SaveToFile(“Encryption.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Encryption.docx”)

        End Sub
    End Class
End Namespace

Permission Settings:

            document.Protect(ProtectionType.AllowOnlyComments);

            document.Protect(ProtectionType.AllowOnlyFormFields)

            document.Protect(ProtectionType.AllowOnlyRevisions);

With permission, we can find that some tools in toolbar are invalid.

Result Shown as Following:

With Password

With Permission

________________________________________________________________________________________

Click Here to Learn more about Spire.Doc

Click Here to Download Spire.Doc

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s