【问题标题】:OpenXml - How to identify whether the paragraph extends to next pageOpenXml - 如何识别段落是否延伸到下一页
【发布时间】:2011-11-17 09:51:51
【问题描述】:

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

如何做到这一点?有没有办法在文档中设置段落中不允许分页符?任何意见都非常感谢。

【问题讨论】:

    标签: openxml page-break


    【解决方案1】:

    一般来说,您不能使用 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;
            }       
        }
    }
    

    【讨论】:

    • 谢谢。你能给我一些关于如何解决这个问题的更多提示吗?
    猜你喜欢
    • 2021-12-25
    • 2015-08-31
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多