Programming

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

January 10, 2011

We may set one password for a file if we don't want others to open it, especially the secrete document. Generally speaking, we are used to writing articles or papers in Microsoft Word. And we may need to encrypt Word document if it is very important for security. Also, when we operate Word by using C#/VB.NET, to encrypt Word document is also needed to make sure that the document cannot be opened unless having the password. So, how should we do to etncrypt Word document in C#/VB.NET?

How to Encrypt Document in Microsoft Word

Firstly, we need to open the document which we want to encrypt. On the tools menu, we can find the Options tab. Click it and select Security tab. Then click the advance button. Choose RC4, Microsoft Enhanced RSA and AES Cryptographic Provider from the Encryption Type menu. The Key Length is set 128 and we need to check the Encrypt document properties. Finally, click OK. Now, we can key a password in the "Password to Open" filed. After clicking OK, we can get anther dialog box which requires us to Re-Enter the password. Re-enter the password and click OK. Please note that it is difficult to recover a password encrypted file once you have lost the password.

How to Encrypt Word Document from Spire.Doc

You just need to invoke the method Encrypt on the document which you want to encrypt, and pass the password as the parameter.

The following code will show you the way to encrypt Word document:

[C#]

using Spire.Doc;

using Spire.Doc.Documents;

namespace Encrypt

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a word document.

            Document document = new Document();

            //Create a new paragraph.

            Paragraph paragraph = document.AddSection().AddParagraph();

            //Append Text.

            paragraph.AppendText("The sample demonstrates how to encryt a document.");

            paragraph.ApplyStyle(BuiltinStyle.Heading4);

            //Input the password.

            document.Encrypt("test");

            //Save doc file.

            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launching the MS Word file.

            System.Diagnostics.Process.Start("Sample.doc");

        }

    }

}

[Visual Basic]
Imports Spire.Doc

Imports Spire.Doc.Documents

Module Module1

    Sub Main()

        'Create word document.

        Dim document As New Document()

        'Create a new paragraph.

        Dim paragraph As Paragraph = document.AddSection().AddParagraph()

        'Append Text.

        paragraph.AppendText("The sample demonstrates how to encryt a document.")

        paragraph.ApplyStyle(BuiltinStyle.Heading4)

        'Encrypt the document.

        document.Encrypt("test")

        'Save the file.

        document.SaveToFile("Sample.doc")

        'Launch the file.

        System.Diagnostics.Process.Start("Sample.doc")

    End Sub

End Module

After running the demo, you may find a dialog box appear in the document, you can input your password into the box.

 

 

How to Use C# to Set Excel Font

January 5, 2011

In order to show a good-looking Excel file or emphasize one part of Excel, different font styles for cells, which include characters, are needed. Generally speaking, the frequently used styles contain font type, size and color.

Also, developers need to set Excel Font Style in C# during the process to operate Excel, the Excel file is com...


Continue reading...
 

Method to Use C#.Net to Set Excel Font

January 4, 2011

When I use Microsoft Word to write an article or paper, I would like to set the font style, for example, setting characters size, color and so on. Generally, I will make the title be larger than other part or give the keywords a specified color to make it be obvious. As the same, we also need to set Excel font for the formatted Excel file ...


Continue reading...
 

Using C# Excel Interop Class

December 28, 2010

Sometimes we need to control c# .net excel class in our program in order to make the Microsoft Office c# excel interop assembly working properly. The application program must process a batch of spreadsheets and deal with excel worksheets. Here I would like to offer some tips on c# excel interop performance and introduce you some fundamen...


Continue reading...
 

Excel Worksheet Object in .Net

December 27, 2010

Excel object plays an important role when we operate Excel in .Net for we often need to use Excel object in .Net to realize Excel automation. There are four Excel object classes frequently used, Application, Workbook, Worksheet and Range. I want to discuss something about Worksheet of Excel object.

As one class of Excel object, Workshee...


Continue reading...
 

C# .Net Used in Excel

December 22, 2010
We often use Excel during working to record or deal with data for it has a powerful function to calculate data. Usually, we export or update data to Excel manually. It will be a big project if there is thousand of information needed to deal with. VBA macro helps users to realize Excel automation by using object and offers kinds a large number of methods and properties of each object class to control Excel. However, it is also a trouble when we want to get data from outside source even if usin...

Continue reading...
 

Way to Save Workbook by SaveAs Method in C#

December 16, 2010

We save an Excel file after creating it. We have many methods to save an Excel file, which is often called workbook. Generally, workbook will be saved in the current folder with the name it was given when it was created if we don’t save it by specifying a path. Also, we can save a copy of the workbook without modifying the open workbook in memory.

The first way is to save a workbook without changing the path associated with a document-level customization.

Call the Save method of the This...


Continue reading...
 

Way to Save Workbook by SaveAs Method in C#

December 16, 2010

We save an Excel file after creating it. We have many methods to save an Excel file, which is often called workbook. Generally, workbook will be saved in the current folder with the name it was given when it was created if we don’t save it by specifying a path. Also, we can save a copy of the workbook without modifying the open workbook in memory.

The first way is to save a workbook without changing the path associated with a document-level customization.

Call the Save method of the This...


Continue reading...
 

Import Excel Data by Integration

December 10, 2010
Data is very important when we create a useful Excel workbook. How do the data come from? Usually, every company has a system database which is used to store a great deal of data, such as finance, staff information and so on. Developers research so much on importing Excel data to make everything become easier. One of the ways to import Excel data is to use Integration service.
There is a good tool to import Excel data which is called SQL Server Integration Service, known as SSIS. Through Excel...
Continue reading...
 

About Me


Recent Posts