'Anil' Radhakrishna's Code Gallery

Home | Blog | Contact
Web Development Tips, Tricks & Trivia

"Download as Word" - Add User Info


This code sample is related to the Office XML and CSS technique of generating a Word document dynamically. If you need user information to be embedded inside the Word document that you generate from code, use the DocumentProperties Element. Detailed info about the DocumentProperties Element can be found in the Microsoft Office HTML and XML Reference. The document properties shown in the sample below are directly borrowed from the DocumentProperties Element sample in the Reference.

Please post any questions you may have in the Comments section of the original Code Project article.

As the output of this program is a Word document rather than HTML, we can use a Generic Handler (ASHX).View Output (ASPX)

<%@ WebHandler Language="C#" Class="Handler" %>
 
using System;
using System.Web;
using System.Text;
 
public class Handler : IHttpHandler
{
 
    public void ProcessRequest(HttpContext context)
    {
        StringBuilder sbTop = new System.Text.StringBuilder();
 
        sbTop.Append(@"
        <html 
        xmlns:o='urn:schemas-microsoft-com:office:office' 
        xmlns:w='urn:schemas-microsoft-com:office:word'
        xmlns='http://www.w3.org/TR/REC-html40'>
        <head><title></title>");
 
        sbTop.Append(@"
        <!--[if gte mso 9]><xml>
        <o:DocumentProperties>
        <o:Subject>Sales Data for this Quarter</o:Subject>
        <o:Author>Kim Abercrombie</o:Author>
        <o:Keywords>Sales Marketing Report</o:Keywords>
        <o:Description>Pending approval</o:Description>
        <o:Template>Contemporary Report.dot</o:Template>
        <o:LastAuthor>Kim Abercrombie</o:LastAuthor>
        <o:Revision>10</o:Revision>
        <o:TotalTime>114</o:TotalTime>
        <o:LastPrinted>1999-05-19T00:49:00Z</o:LastPrinted>
        <o:Created>1999-05-18T22:32:00Z</o:Created>
        <o:LastSaved>1999-05-19T00:52:00Z</o:LastSaved>
        <o:Category>Reports</o:Category>
        <o:Manager>Kate Dresen</o:Manager>
        <o:Company>Adventure Works</o:Company>
        <o:Version>9.2720</o:Version>
        </o:DocumentProperties>
        </xml><![endif]-->");
 
        sbTop.Append(@"
        <!--[if gte mso 9]>
        <xml>
        <w:WordDocument>
        <w:View>Print</w:View>
        <w:Zoom>90</w:Zoom>
        </w:WordDocument>
        </xml>
        <![endif]-->");
 
        sbTop.Append(@"<style>
        <!-- /* Style Definitions */
        @page Section1
        {size:8.5in 11.0in; 
        margin:1.0in 1.25in 1.0in 1.25in ; 
        mso-header-margin:.5in; 
        mso-footer-margin:.5in; mso-paper-source:0;}
        div.Section1
        {page:Section1;}
        -->
        </style></head>");
 
        sbTop.Append(@"<body lang=EN-US style='tab-interval:.5in'>
        <div class=Section1>
        <h1>Time and tide wait for none</h1>
        <p style='color:red'><I>" + DateTime.Now + "</I></p></div>");
 
        sbTop.Append(@"</body></html>");
 
        string strBody = sbTop.ToString();
 
        context.Response.AppendHeader("Content-Type", "application/msword");
        context.Response.AppendHeader("Content-disposition", "attachment; filename=DownloadAsDocWithUserInfo.doc");
        context.Response.Write(strBody);
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
 
}