OpenXml - 如何识别段落是否延伸到下一页

时间:2011-11-17 09:51:52

标签: openxml page-break

从aspx页面,我使用OpenXml SDK动态地将段落添加到word文档中。在这种情况下,不允许段落中的分页符。因此,如果一个段落在第1页的中间开始并延伸到page2,那么它应该实际上从Page2开始。但是,如果它在同一页面结束,那就没关系。

如何实现这一目标?有没有办法在文档中设置段落中不允许分页?任何意见都非常感谢。

1 个答案:

答案 0 :(得分:2)

通常,你不能使用open xml sdk来确定页面中元素的显示位置,因为open xml没有页面概念。

页面由使用open xml文档的客户端应用程序确定。但是,您可以指定段落的行保持在一起。

<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:keepLines />
  </w:pPr>
  <w:bookmarkStart w:name="_GoBack" w:id="0" />
  <w:r>
    <w:lastRenderedPageBreak />
    <w:t>Most controls offer a choice of using the look from the current theme or using     a format that you specify directly. To change the overall look of your document, choose new your document.</w:t>
  </w:r>
  <w:bookmarkEnd w:id="0" />
</w:p>
上面示例段落属性中的

w:keepLines 是确保段落不在页面之间拆分的关键,下面是生成上述paragrpah所需的open xml:

using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;

namespace GeneratedCode
{
    public class GeneratedClass
    {
        // Creates an Paragraph instance and adds its children.
        public Paragraph GenerateParagraph()
        {
            Paragraph paragraph1 = new Paragraph();

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            KeepLines keepLines1 = new KeepLines();

            paragraphProperties1.Append(keepLines1);
            BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };

            Run run1 = new Run();
            LastRenderedPageBreak lastRenderedPageBreak1 = new LastRenderedPageBreak();
            Text text1 = new Text();
            text1.Text = "Most controls offer a choice of using the look from the current theme or using.";

            run1.Append(lastRenderedPageBreak1);
            run1.Append(text1);
            BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(bookmarkStart1);
            paragraph1.Append(run1);
            paragraph1.Append(bookmarkEnd1);
            return paragraph1;
        }       
    }
}