本文介绍了OpenXML:添加新段落时插入 lastRenderedPageBreak的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 OpenXML 添加一个新段落,如下所示:

I add a new paragraph with OpenXML like this:

var pSpacerAfterSectorTitle = insertNodeSectorsArea.AppendChild(GetNewParagraph("Arial", 12, true, false, null));

方法如下:

private static Paragraph GetNewParagraph(string fontName, int fontSize, bool useBold, bool useUnderline, string text)
{
    var p = new Paragraph();
    var pPr = p.AppendChild(new ParagraphProperties());
    var rPrP = pPr.AppendChild(GetNewRunProperties(fontName, fontSize, useBold, useUnderline));
    var r = p.AppendChild(new Run());
    var rPr = r.AppendChild(GetNewRunProperties(fontName, fontSize, useBold, useUnderline));
    if (!string.IsNullOrEmpty(text))
    {
        r.AppendChild(new Text() { Text = text,
            Space = SpaceProcessingModeValues.Preserve });
    }
    return p;
    }
}

还有……

private static RunProperties GetNewRunProperties(string fontName, int fontSize, bool useBold, bool useUnderline)
    {
        string fontSizeVal = (fontSize * 2).ToString();

        var runPr = new RunProperties();
        runPr.AppendChild(new RunFonts() { ComplexScript = fontName });
        if (useBold)
        {
            runPr.AppendChild(new Bold());
        }
        runPr.AppendChild(new FontSize() { Val = fontSizeVal });
        runPr.AppendChild(new FontSizeComplexScript() { Val = fontSizeVal });
        if (useUnderline)
        {
            runPr.AppendChild(new Underline() { Val = UnderlineValues.Single });
        }
        runPr.AppendChild(new Languages() { EastAsia = "de-CH" });

        return runPr;
    }

当以这种方式添加段落时,我会突然在第一页上得到一个 lastRenderedPageBreak,某个地方与我认为插入段落的位置无关.

When the paragraph is added this way I will suddenly get a lastRenderedPageBreak on the first page, somewhere unrelated to where I insert the paragraph in my opinion.

...<w:sz w:val="18"/><w:szCs w:val="18"/></w:rPr><w:lastRenderedPageBreak/><w:t>Sitzung 20171207 PSC 1</w:t></w:r></w:p>

如果我注释掉段落的添加,则生成的文档不包含此分页符:

If I comment out the adding of the paragraph the generated document does not contain this page break:

...<w:b/><w:sz w:val="18"/><w:szCs w:val="18"/></w:rPr><w:t>Sitzung 20171207 PSC 1</w:t></w:r></w:p>

我多次检查我的代码,但我没有手动创建这样的 lastRenderedPageBreak - 所以我目前不知道它来自哪里.

I checked my code many times but nowhere I create such lastRenderedPageBreak manually - so I currently don't have any idea where it is coming from.

有什么想法吗?

推荐答案

在 OOXML 中,w:lastRenderedPageBreak 旨在由最后一个对文档进行分页的软件插入以标记其分页结果计算.

In OOXML, w:lastRenderedPageBreak is intended to be inserted by the last software that paginated the document to mark the results of its pagination calculations.

因此,为了直接回答您的问题,w:lastRenderedPageBreak 被插入到您的背后,而不是您自己的代码.

So, to answer your question directly, w:lastRenderedPageBreak is being inserted behind your back, not by your own code.

您可以通过不进行分页的库从 MS Word 中操作 OOXML 以避免这种行为,但请注意 w:lastRenderedPageBreak 元素可能会在下次呈现文档时插入并由分页软件保存.

You can manipulate OOXML away from MS Word via a library that doesn't do pagination to avoid this behavior, but be aware that w:lastRenderedPageBreak elements may later be inserted when the document is next rendered and saved by software that does pagination.

另请注意,不幸的是w:lastRenderedPageBreak在准确标记分页方面非常不可靠.

Note also that unfortunately w:lastRenderedPageBreak is quite unreliable in marking pagination accurately.

这篇关于OpenXML:添加新段落时插入 lastRenderedPageBreak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 01:28