开发者

Insert a page break in a generated HTML .doc

I am currently generating a .doc file as html using asp.NET.

I wish to insert a page break to the page but don't know how.

I've tried using the css style='page-break-before:always' but it does nothing.

This is the code assigned to a button click event:

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset ="";

    HttpCo开发者_开发问答ntext.Current.Response.ContentType ="application/msword";

    string strFileName = "GenerateDocument"+ ".doc";
    HttpContext.Current.Response.AddHeader("Content-Disposition","inline;filename=" + strFileName);

    StringBuilder strHTMLContent = new StringBuilder();

    strHTMLContent.Append("<p align='Center'>Content Before Page Break</p>".ToString());

    strHTMLContent.Append("<br><br>".ToString());
    strHTMLContent.Append("<p class='pageBreak' style='mso-special-character:line-break;'>meh</p>".ToString());

    strHTMLContent.Append("<br><br>".ToString()); 
    strHTMLContent.Append("<p align='Center'>Content After Page Break</p>".ToString());

    HttpContext.Current.Response.Write(strHTMLContent);
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();


Word has its own special classes. mso-special-character:page-break; does work in this situation, as mentioned by Pekka, however what is missing is this should be on a <br> tag. So the following code will work:

<br clear=all style='mso-special-character:line-break;page-break-before:always'>

It's not particularly nice solution, but in my case I had no choice (I didn't write the original code, and it was a much bigger job to change it).


I don't know what Office's notation for that is, but I would create a document with a page break in Word, save it as HTML, and look what it uses.

Guessing from the code block you show, it could be

mso-special-character:page-break;


I would use the Gios.Word .NET library instead. Much simpler to create RTF compatible word files with tables and page breaks etc.

I tried to create XML based Doc files first and found that Gios is better:

Here is an article about it:

http://www.codeproject.com/KB/string/gioswordnetlibrary.aspx

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=11252


If anyone comes across this and the accepted answer isn't possible or CodePB's answer doesn't work, use the following. I simply took 'line-break' out and replaced it with 'page-break' and this seemed to work out of the box.

<br clear=all style='mso-special-character:page-break;page-break-before:always'>


Don't generate MS Word files with HTML

You wouldn't want to build a house with the plans for a boat would you?

If you're wanting to do this programmatically I suggest you either take a look at the docx format or even the rich text format specification and move from there. But in all likelyhood you're going down the wrong path and theres already libraries out there that you can use.

I suggest you start here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜