delia99 0 Newbie Poster

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 <URL SNIPPED>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 <URL SNIPPED>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:

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"); 

         } 

     } 

 }
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

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