Export To Microsoft Word using XML / HTML - Problem with Headers and Footers

247 views
Skip to first unread message

Jared

unread,
Feb 19, 2008, 6:58:33 AM2/19/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi

I have written a user control to export HTML to Word. This is working
great, except for one minor problem.

If I put plain text in the header or footer, it also appears on the
the main page. I suspect there is a way around this using the tags
mentioned below, but I am struggling to find a solution. I really
don't want to have to create a separate Header.htm file if possible.


"Word does not display header and footer information in an HTML page,
but it does store the data.

Header and footer information is stored in a separate file named
Header.htm. Inside the file, the header and footer information is
stored as fields, using Span elements with the mso-element:field-
begin, mso-element:field-separator, and mso-element:field-end
attributes in conjunction with the MsoHeader and MsoPageNumber
styles."


My method is below, the text in the 'Header', 'Footer' and 'Page # of
#' show in the body, the page numbers don't. These are the specific
lines in question;

//Header
strBody.Append("<div style='mso-element:header;' id=h1><p
class=MsoHeader><span style='mso-tab-count:1'></span>" + Header + "</
p></div>");

//Footer
strBody.Append("<div style='mso-element:footer;' id=f1><p
class=MsoFooter>" + Footer + "<span style='mso-tab-count:1'></
span>Page <span style='mso-field-code:\" PAGE \"'></span> of <span
style='mso-field-code:\" NUMPAGES \"'></span></p></div></body></
html>");


Any help here would be much appreciated. Credit for this code goes to
MV ARK(http://www.eggheadcafe.com/tutorials/aspnet/6e1ae1a8-8285-4b2a-
a89b-fafc7668a782/aspnet-download-as-wor.aspx).

Thanks

Jared


public override string GetOutput()
{
//build the content for the dynamic Word document
//in HTML alongwith some Office specific style
properties.
StringBuilder strBody = new StringBuilder("");

strBody.Append("<html ");
strBody.Append("xmlns:o='urn:schemas-microsoft-
com:office:office' ");
strBody.Append("xmlns:w='urn:schemas-microsoft-
com:office:word' ");
strBody.Append("xmlns='http://www.w3.org/TR/REC-
html40'>");
strBody.Append("<head><title></title>");

//The setting specifies document's view after it is
downloaded as Print Layout
//instead of the default Web Layout. For the Header &
Footer to be visible,
//this mode is required.
strBody.Append("<!--[if gte mso
9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>90</
w:Zoom><w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]--
>");

//we can tweak the MsoFooter and MsoHeader classes that
are referenced by the header and footer, as required
strBody.Append("<style><!-- /* Style Definitions */
p.MsoFooter, li.MsoFooter, div.MsoFooter{margin:0in;margin-bottom:.
0001pt;mso-pagination:widow-orphan;tab-stops:center 3.0in right
6.0in;font-size:8.0pt;}");
strBody.Append("<style><!-- /* Style Definitions */
p.MsoHeader, li.MsoHeader, div.MsoHeader{margin:0in;margin-bottom:.
0001pt;mso-pagination:widow-orphan;tab-stops:center 3.0in right
6.0in;font-size:8.0pt;}");


//Word uses the @page definition to store document layout
settings for the entire document.
//Using @page SectionN, Word applies page formatting to
individual HTML elements referenced
//through the class attribute.
//mso-footer is the style attribute related to the footer
//Refer to the topic "Page Layout and Section Breaks" &
"Headers and Footers" in the
//Office XML & HTML Reference for detailed info.
strBody.Append("@page Section1 {size:8.5in 11.0in; margin:
1.0in 1.25in 1.0in 1.25in; mso-header: h1; mso-header-margin:.5in; mso-
footer: f1; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1
{page:Section1;} --> </style></head>");

strBody.Append("<body lang=EN-US style='tab-interval:.5in;
font-family:Arial;'><div class=Section1><p style='color:black; font-
size:15pt; font-weight:bold;'>" + Title + "</p><p style='color:gray;
font-size:12pt; font-weight:bold; font-style:italic;'>" + SubTitle +
"</p>");

//We are building up a big string here so that the
generated doc runs into multiple pages.
foreach (string mySection in Body)
{
strBody.Append(mySection);
strBody.Append("<br>");
}

strBody.Append("</div>");


//Word marks and stores information for simple fields by
means of the Span element with the
//mso-field-code style. Refer to the topic "Fields" in the
Office XML & HTML Reference

//Header
strBody.Append("<div style='mso-element:header;' id=h1><p
class=MsoHeader><span style='mso-tab-count:1'></span>" + Header + "</
p></div>");

//Footer
strBody.Append("<div style='mso-element:footer;' id=f1><p
class=MsoFooter>" + Footer + "<span style='mso-tab-count:1'></
span>Page <span style='mso-field-code:\" PAGE \"'></span> of <span
style='mso-field-code:\" NUMPAGES \"'></span></p></div></body></
html>");

//Get the Output
Output = strBody.ToString();
return Output;
Reply all
Reply to author
Forward
0 new messages