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

            // Check if ToWord exists in the query string parameters
            // 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
            // Complete the Request
            // End the response.
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.

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

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

Hope this helps,

Published 08 September 2008 10:53 AM by haissam


# BasharKokash said on 08 September, 2008 03:42 AM

Thanks Haissam, that was very usefull.

# BasharKokash said on 08 September, 2008 03:42 AM

Thanks Haissam, that was very usefull.

# haissam said on 08 September, 2008 04:28 AM

Your more than welcomed Bashar

# kaushalparik said on 08 September, 2008 04:33 AM

thanx haissam, that is a useful functionality indeed.  

# SHAMA84 said on 08 September, 2008 07:53 AM

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  ???

# mosessaur said on 08 September, 2008 06:10 PM

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.


# haissam said on 10 September, 2008 03:57 AM

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

# Abhishek K R said on 10 October, 2008 04:05 AM

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. :)

# haissam said on 10 October, 2008 04:31 AM

Send me an email in order to send you the httpmodule

# Panoone said on 01 October, 2013 12:59 AM

This sends the HTML as a string only? How can you embed any required image references and assign specific styles or remove certain content by ID (such as header, navbar etc?

This site



This Blog



  • MaximumASP