'Anil' Radhakrishna's Code Gallery

Home | Blog
Web Development Tips, Tricks & Trivia

Generate a Word document dynamically with user submitted text formatted with Free Text Box

This code sample show how to generate a Word document dynamically with user submitted text formatted with a rich text editor control like Free Text Box. By cross posting the formatted text (captured through FreeTextBoxdemo.aspx) to a separate Word file generation page (GenerateWordDoc.aspx) the HTML content that makes up the Word document is cleaner. Note that although the code below doesn't do it, any input submitted to the server ( specially because ValidateRequest="false" ) should be sanitized to prevent any cross-site scripting (XSS) attacks.

FreeTextBoxdemo.aspx:
<%@ Page Language="C#"  ValidateRequest="false"%>

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Generate a Word document dynamically with user submitted text formatted with 
    Free Text Box</title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>Please provide feedback:
        <FTB:FreeTextBox  ID="FreeTextBox1" runat="server">
        </FTB:FreeTextBox>
    
    </div>
        <asp:Button  ID="Button1" runat="server" Text="Button" 
        PostBackUrl="GenerateWordDoc.aspx" />
    </form>
</body>
</html>


GenerateWordDoc.aspx:
<%@ Page Language="C#" ValidateRequest="false" %>

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();

        sb.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>Time</title>");

        sb.Append(@"<!--[if gte mso 9]>
                         <xml>
                         <w:WordDocument>
                         <w:View>Print</w:View>
                         <w:Zoom>90</w:Zoom> 
                         <w:DoNotOptimizeForBrowser/>
                         </w:WordDocument>
                         </xml> 
                        <![endif]-->");

        sb.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-page-orientation: landscape; 
                     mso-footer-margin:.5in; mso-paper-source:0;}
                    div.Section1
                    {
                     page:Section1;
                    }
                    -->
                   </style></head>");

        sb.Append(@"<body lang=EN-US style='tab-interval:.5in'>
                            <div class=Section1>
                            <b>The customer is always right</b> - handed<br>");
        sb.Append(@"<p>Customer Comments:<br>" + 
                 ((FreeTextBoxControls.FreeTextBox)(PreviousPage.FindControl("FreeTextBox1"))).Text 
                 + "</p>");
        sb.Append(@"<hr><p style='color:red'><I>Document generated on " +  DateTime.Now + 
                   "</i></div></body></html>");
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader("Content-disposition", "inline; filename=comments.doc");
        Response.Write(sb);
    }
 </script>

Read related blog posting
Code formatted with Thomas Johansen's utility..