How to Encrypt Word Document
If you want to protect your sensitive data in Word document from others, you can just encrypt Word document by adding password. Adding security to Word document by making document password-protected is the most usual method to protect Word document file. No one will be able to open your file without the password you set. The function of encrypting Word document is fully supported by Microsoft Word. But here in this post you will find another solution to encrypt Word document - Use C# to encrypt Word document.
How to Encrypt Word Document via C#
By using Spire.Doc, we can use C# to encrypt Word document with a few minutes. Spire.Doc is a professional .NET Word document which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. Download Spire.Doc and make sure it correctly installed on system with Visual Studio. Follow the simple guide below to encrypt Word document.
Step 1 Create Project in Visual Studio
Create a C# windows form project in visual studio. Drag a button on and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace EncryptWord_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Load Word Document
Use the code below to load Word document which we will add security on.
Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");
Step 3 Add Textbox
Add a textbox into the windows form. We will need that textbox to set password.
document.Encrypt(this.textBox1.Text);
How to Encrypt Word Document via C#
By using Spire.Doc, we can use C# to encrypt Word document with a few minutes. Spire.Doc is a professional .NET Word document which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. Download Spire.Doc and make sure it correctly installed on system with Visual Studio. Follow the simple guide below to encrypt Word document.
Step 1 Create Project in Visual Studio
Create a C# windows form project in visual studio. Drag a button on and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace EncryptWord_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Load Word Document
Use the code below to load Word document which we will add security on.
Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");
Step 3 Add Textbox
Add a textbox into the windows form. We will need that textbox to set password.
document.Encrypt(this.textBox1.Text);
Step 4 Save Project
After setting password, use the code below to save the edited Word document. We can rename it here.
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Effective Screenshot:
After setting password, use the code below to save the edited Word document. We can rename it here.
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Effective Screenshot:
More about Spire.Doc
As a professional and powerful Word component, Spire.Doc doesn’t need Microsoft Office Word Automation but also allows user to directly operate Word document, format and style and insert content to Word document. Spire.Doc which with high quality, powerful functions and cheap price supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more…
As a professional and powerful Word component, Spire.Doc doesn’t need Microsoft Office Word Automation but also allows user to directly operate Word document, format and style and insert content to Word document. Spire.Doc which with high quality, powerful functions and cheap price supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more…