Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Margins on HTML export to Word

The actual export is no problem:

Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/msword";
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Content-Type", "application/word");
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.doc", docName));
Response.Charset = "utf-8";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
mainbody.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

So my question is: How do I change the margins on the resulting word doc?

If I open the downloaded word doc then save as HTML, the directive that defines the margins is:

@page WordSection1
{
  size:8.5in 11.0in;
  margin:1.0in 1.0in 1.0in 1.0in;
  mso-header-margin:.5in;
  mso-footer-margin:.5in;
  mso-paper-source:0;
}    

I'm wondering how I can change this in the response headers so I can output the form correctly on one sheet. Any ideas?

like image 383
TomO Avatar asked Nov 22 '11 19:11

TomO


People also ask

Can I convert HTML to Word?

You will need to use Microsoft Word to perform this process; luckily, Word will automatically convert an HTML document into its web page format when you open the HTML document in Word.

How do I fix messed up Margins in Word?

To change margins, click on the Margins button, found on the Page Layout tab. Word lists a number of pre-formatted options, but you can also make your own margins by selecting “Custom Margins,” found at the bottom of the Margins list. You can change each of the four margins in the dialog box that appears.


1 Answers

Here is how I solved this, in VB.NET rather than C#. I got the code from here:

divContent.RenderControl(New HtmlTextWriter(New IO.StringWriter(sb)))

memoBody = sb.ToString

Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" & fileName & "")
Response.ContentType = "application/msword"
Dim sbResponseString As New StringBuilder
Dim wordHeader as String = "<html xmlns:o=""urn:schemas-microsoft-com:office:office"" "
wordHeader &= "xmlns:w=""urn:schemas-microsoft-com:office:word"" "
wordHeader &= "xmlns=""http://www.w3.org/TR/REC-html40""> "
wordHeader &= "<head><title>Document Title</title>"
wordHeader &= "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>"
wordHeader &= "<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->"
wordHeader &= "<style> @page Section1 {size:8.5in 11.0in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; "
wordHeader &= "border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; "
wordHeader &= "margin:0.75in 0.50in 0.75in 0.50in ; mso-header-margin:.5in; "
wordHeader &= "mso-footer-margin:.5in; mso-paper-source:0;} "
wordHeader &= "div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, "
wordHeader &= "div.MsoFooter{margin:0in; margin-bottom:.0001pt; "
wordHeader &= "mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; "
wordHeader &= "font-size:12.0pt; font-family:'Arial';} "
wordHeader &= "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; "
wordHeader &= "margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center "
wordHeader &= "3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}--></style></head> "

Dim wordBody as String = "<body><div class=Section1>" & memoBody & "</div></body></html>"

sbResponseString.Append(wordHeader)
sbResponseString.Append(wordBody)

Dim newResponseString As String = sbResponseString.ToString
Response.Write(newResponseString)
Response.End()
like image 150
David Brunow Avatar answered Oct 04 '22 07:10

David Brunow