MAC
MAC

Reputation: 6577

Protecting word Document with Password

I have created one asp.net application which has one functionality of conversion of Html table to word Document. I had done this successfully. But now i will need to protect my file with password. Is it possible to make the file as secure by adding password. My conversion code is shown below.

Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", DateTime.Today.ToShortDateString().Replace("/", "").Replace("-", "") + "_" + DateTime.Now.ToShortDateString() + ".doc"));
        Response.ContentType = "application/ms-word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        tblMain.RenderControl(htw);

        string strPath = Request.PhysicalApplicationPath + "Test.doc";
        StreamWriter sWriter = new StreamWriter(strPath);
        sWriter.Write(sw.ToString());
        sWriter.Close();

Please help me, if there any way to add password to the saving files. Thanks in advance..

Upvotes: 0

Views: 3918

Answers (2)

Aaron Caito
Aaron Caito

Reputation: 98

This is C# but looks to be the properties you are looking for.

ThisDocument.SaveAs("c:\test\MyNewDocument.doc")

// C#
Object fileName = @"C:\Test\MyNewDocument.doc";
Object password = Type.Missing;
Object writePassword = Type.Missing;

ThisDocument.SaveAs(ref fileName, ref password, ref writePassword);

http://msdn.microsoft.com/en-us/library/office/aa192495(v=office.11).aspx

Upvotes: 1

Sandeep
Sandeep

Reputation: 5771

Word.Document allows you to save the document by passing the password as a parameter. See this.

Upvotes: 1

Related Questions