Skip to content

Instantly share code, notes, and snippets.

@mvark
Created January 8, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvark/96a971cf193e8f13142f to your computer and use it in GitHub Desktop.
Save mvark/96a971cf193e8f13142f to your computer and use it in GitHub Desktop.
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 g…
<%@ 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment