【发布时间】:2014-05-09 22:08:32
【问题描述】:
提前感谢您提供的任何帮助。我一直在挖掘关于 SO 的各种帖子,试图解决缺少的分页符问题。我有许多 cshtml 模板,它们使用 Razor 模板引擎呈现以生成 RTF 文件。当在 Word 中打开文件时,它会完全转储包含用于分页的“break”样式的 html。模板的其余部分完全按照设计呈现。我在想,也许我的问题是我一开始没有正确渲染文件。
示例 .cshtml 文件开头为:
<div style="width: 100%;" class="newpage">
<p align="center" style="text-align:center">NOTICE</p>
<table>
....middle html content....
</table>
<p class="newpage"></p>
</div>
我尝试将分页符放在开头和结尾,只是想看看它是否有任何区别,但没有。
获取.cshtml文件的razor调用是这样的:
newNotice.Body = RazorEngine.Razor.Parse(Notifications.GetTemplate(NotificationTemplate.MyTemplate2), data, NotificationTemplate.MyTemplate2.ToString());
return newNotice;
然后我在以下代码中渲染出 RTF 文件,其中存在分页符样式:
protected void ProduceNotification(Notification notice, string title)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + title);
Response.ContentType = "application/rtf";
EnableViewState = false;
var stringWrite = new StringWriter();
var sb = new StringBuilder();
sb.Append(@"<html>
<head>
<title></title>
<style type='text/css'>
.body {
margin: 5em;
background-color: white;
width: 100%;
height: 100%;
font-family: Calibri;
}
.spacing {
line-height: 1.5em;
}
.newpage {
mso-special-character:line-break;
page-break-before:always;
clear: both;
}
</style>
</head><body>");
sb.Append(notice.Body);
sb.Append("</body></html>");
stringWrite.WriteLine(sb);
ClearChildControlState();
Response.Write(stringWrite.ToString());
Response.End();
}
当我在 Word 中打开文件时,没有考虑分页符,因此我将文件保存到 .html 以查看标记的样子,这就是我看到的:
在样式定义部分它显示了样式:
p.newpage, li.newpage, div.newpage
{mso-style-name:break;
mso-style-unhide:no;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
page-break-before:always;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman","serif";
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;}
但是在 html 应该显示带有类的元素的文档中,它看起来像这样(这是在 html 输出的中间):
<div>
<p align=center style='text-align:center'>NOTICE </p>
<table>
......middle html content.....
</table>
</div>
请注意,div 标记中缺少样式和类,而 p 标记一起丢失。我想不通的是,转换为 RTF 是出于某种原因删除了这些内容,还是 Razor 模板引擎删除了文本?我也尝试将其更改为输出 .doc 格式,但结果是相同的。
【问题讨论】:
-
首先放置一个断点并检查您要添加到字符串生成器的正文字符串。