.NET Word Examples
- Create Word Document
- Perform Mail Merge
- Manage Bookmarks
- Find and Replace Text
- Format Tables
- Split Word Documents
- Merge Multiple Word Documents
- Fill and Manage Form Fields
- Generate and Update Table of Contents
- Compare Word Documents
- Encrypt and Decrypt Documents with Password
- Convert Word Documents to PDF
- Convert Word Documents to Images
- Word-HTML Conversions
- Word-Markdown Conversions
- Word-Text Conversions
Encrypt and Decrypt Word Documents Using the Syncfusion .NET Word Library
Protect your sensitive information and control document access with ease. The Syncfusion® .NET Word Library enables you to programmatically encrypt and decrypt Word documents in C#, all without relying on Microsoft Word or interop dependencies.
Watch this video to learn how to encrypt and decrypt Word documents using the Syncfusion .NET Word Library.
Encrypt and decrypt Word document using C#
Learn how to encrypt and decrypt Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates password-based protection to secure documents and remove encryption to make them accessible.
Step 1: Create a new project
Start by creating a new C# Console Application project.
Step 2: Install the NuGet package
Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.
Step 3: Add required namespaces to encrypt and decrypt Word document
Add the following namespaces to your Program.cs file:
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;Step 4: Open a Word document
Open an existing Word document from a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)
//Open the template document
WordDocument document = new WordDocument(fileStream, FormatType.Docx);Step 5: Encrypt the Word document
Apply password-based encryption to protect the Word document.
//Encrypt the Word document with a password
document.EncryptDocument("syncfusion");Step 6: Save the encrypted Word document
Save the encrypted Word document to a file stream.
//Create file stream
FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)
//Save the Word document to file stream
document.Save(outputStream, FormatType.Docx);
document.Close();
fileStream.Close();
outputStream.Close();Step 7: Open an encrypted Word document
Open an encrypted Word document using the password to access its contents.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite);
//Open an encrypted Word document
WordDocument document = new WordDocument(fileStream, FormatType.Docx, "syncfusion");Step 8: Decrypt the Word document
Remove the encryption from the Word document to make it accessible.
//Remove encryption in Word document
document.RemoveEncryption();Step 9: Save the decrypted Word document
Save the decrypted Word document to a file stream.
//Create file stream
FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite);
//Save the Word document to file stream
document.Save(outputStream, FormatType.Docx);
document.Close();
fileStream.Close();
outputStream.Close();GitHub project
NuGet installation
Get started quickly by downloading the installer and checking license information on the Downloads page.
Table of contents
Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.
Learning
Technical Support
