How to Encrypt Word Document with Silverlight

Because of the sharp development of internet, information can be spread very quickly. However, some people find that their documents which are submitted online are stolen or modified by others. Therefore, it is very important to protect electronic documents.

Actually, there are several methods to protect a document. Some people encrypt document directly by setting a password. If so, others cannot view document contents unless they have the correct password. Some others just set permissions for document. For example, readers can view and print document but cannot edit.

In this post, I will introduce a method about how to encrypt Word document with Silverlight. I will set a password for one existed document. If you want to view it, you must enter the password.

Note: component, Spire.Doc for Silverlight is used in this example. So I have added its dll file as reference in project.

STEPS

Step 1. Design UserControl

Rename MainPage.xaml as Encryption.xaml. Then, double click it to design UserControl. I change background color at first. Next, add a label to show what I will do. Set label background color and set text format. Finally, add a button to run.

Step 2. Declare saveFileDialog

Declare a saveFiledialog for saving Word document. Then, set filter for this saveFileDialog, which is used for choosing Word format (.doc or .docx). In this example, I just set the format as .docx.

        private SaveFileDialog saveFiledialog = new SaveFileDialog();
        public MainPage()
        {
            InitializeComponent();
            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
        }

Step 3. Load Document

Right click project name to add existed item which is the Word document I want to encrypt. After adding, find the document and click it. Change its Build Action as Embedded Resource.

Double click run button to write code. Declare a document at the beginning. Then, get embedded resource from assembly and load it.

            Document document = new Document();

            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“Student Transcript.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }

Step 4. Encrypt and Save Document

Use document.Encrypt() method to encrypt this document. The parameter passed to this method is password string.

Then, judge if the saveFileDialog which I declare at step 1 can pop up. If yes, save the encrypted document through it.

            document.Encrypt(“123456789”);

            bool? result = this.saveFiledialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFiledialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }

Full Encryption.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.IO;
using Spire.Doc;

namespace EncryptWord
{
    public partial class MainPage : UserControl
    {
        private SaveFileDialog saveFiledialog = new SaveFileDialog();
        public MainPage()
        {
            InitializeComponent();
            this.saveFiledialog.Filter = “Word Document (*.docx)|*.docx”;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith(“Student Transcript.docx”))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            document.Encrypt(“123456789”);

            bool? result = this.saveFiledialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = this.saveFiledialog.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }
        }
    }

RESULT

______________________________________________________________________________

Click Here to LEARN MORE about Spire.Doc for Silverlight

Click Here to DOWNLOAD Spire.Doc for Silverlight

Spire.Office also can be used to realize this function

Advertisements

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