1

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

如何做到这一点?有没有办法在文档中设置段落中不允许分页符?任何输入都受到高度赞赏。

4

1 回答 1

2

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

页面由使用打开的 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 所需的打开 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;
        }       
    }
}
于 2011-11-18T09:21:39.957 回答