【问题标题】:Cshtml to RTF page break disappears when file opens in Word在 Word 中打开文件时 Cshtml 到 RTF 分页符消失
【发布时间】: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 格式,但结果是相同的。

【问题讨论】:

  • 首先放置一个断点并检查您要添加到字符串生成器的正文字符串。

标签: html asp.net razor


【解决方案1】:

因此,加重的答案也被证明是最简单的答案。该样式应用于 p、li 或 div 元素,但前提是元素具有某种内容:

 <div class='newpage'>&nbsp;</div>

只要我使用了正确的元素之一,只要有某种内容(在这种情况下只是一个空格),它就可以工作。一旦我让它工作,我还必须解决它在输出中添加一个额外的空白页的问题,因为最初我在 cshtml 文件本身中有该行。相反,我将其移至后面的代码。

我创建了一个 var 来保存最后一个列表项,如 this SO post here 中所示。

var lastItem = myList.Last();

foreach(var d in myList)
{
    newNotice.Body += RazorEngine.Razor.Parse(Notifications.GetTemplate(NotificationTemplate.MyTemplate2), data, NotificationTemplate.MyTemplate2.ToString());
    if(!ReferenceEquals(d, lastItem))
        newNotice.Body += "<div class='newpage'>&nbsp;</div>";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多