Convert ASPX Pages to Word Document

 Convert ASPX Webforms To Word

We will discuss how to convert an ASPX page to word document and send it to the client. We will create an HttpModule which use the Application_BeginRequest event to check if a specific query string parameter exists to render the page into a word file.
First of all, Create a new class library project "WordConverter", Rename the class into "Converter" and let it implement IhttpModule interface which exists in System.Web namespace (you need to add it to your references before being able to add it to your namespace collection)

         public class Converter : IHttpModule

After this stage, use the Init() to register for the Application_BeginRequest event.

        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(app_BeginRequest);
        }
        void app_BeginRequest(object sender, EventArgs e)
        {
 }
 // Dispose should be added
        public void Dispose()
 {
 }

In the app_BeginRequest event you will need to write the below code

            HttpContext.Current.Response.Clear();
            // Check if ToWord exists in the query string parameters
            if(HttpContext.Current.Request.QueryString["ToWord"]!=null)
            {
            // Set the buffer to true
            HttpContext.Current.Response.Buffer = true;
            // Create a new HttpWebRequest to the same url
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(HttpContext.Current.Request.Url.ToString().Split('?')[0]);
            // Set the credentials to default credentials: used if you are under proxy server
            webRequest.Credentials = CredentialCache.DefaultCredentials;
            // Get the response as stream
            Stream stream = webRequest.GetResponse().GetResponseStream();
            // Set the content type of the response on msword
            HttpContext.Current.Response.ContentType = "application/msword";
            // Read the response as string
            string pageHTML = new StreamReader(stream).ReadToEnd();
            // Write it to the response
            HttpContext.Current.Response.Write(pageHTML.ToString());
            // Complete the Request
            ((HttpApplication)sender).CompleteRequest();
            // End the response.
            HttpContext.Current.Response.End();
            }
P.S: Make sure you added the below namespaces
using System.Net;
using System.Web;
using System.IO;

After finishing implementing the code, compile your project. Now you need to add this module to your web application by adding it to your references and add the below code into the web.config to register the module.

    <httpModules>
        <add type="WordConverter.Converter" name="Converter"/>
      </httpModules>

You can find the Module solution attached to this blog post.

Hope this helps,

Comments

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 3:42 AM by BasharKokash

Thanks Haissam, that was very usefull.

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 3:42 AM by BasharKokash

Thanks Haissam, that was very usefull.

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 4:28 AM by haissam

Your more than welcomed Bashar

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 4:33 AM by kaushalparik

thanx haissam, that is a useful functionality indeed.  

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 7:53 AM by SHAMA84

Thanks alot for all ...

but i have a problem is that ..i had make a refernce to that class ,

but firstly, i can't use tat to convert a current page..

Secondly, what is HttpApplication  ???

# re: Convert ASPX Pages to Word Document

Monday, September 08, 2008 6:10 PM by mosessaur

Really good tip. I would appreciate if you could format the source code in the post, would make it easy to read the code online.

Thank you.

Cheers

# re: Convert ASPX Pages to Word Document

Wednesday, September 10, 2008 3:57 AM by haissam

SHAMA it is an httpmodule, you need to plug it into your application. I already demonstrated how to do it at the final lines of code.

mosessaur thank you, but if you could be more specific how to reformat the source code it would be great

# re: Convert ASPX Pages to Word Document

Friday, October 10, 2008 4:05 AM by Abhishek K R

Hi haissam,

Can I get the Module solution from you. As the zip file I dowloaded is throwing some error.

Its really urgent. I am in between something very important.

Could you please help. :)

# re: Convert ASPX Pages to Word Document

Friday, October 10, 2008 4:31 AM by haissam

Send me an email in order to send you the httpmodule

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.