Patric
Patric

Reputation: 2989

OpenXML: lastRenderedPageBreak inserted when adding new paragraph

I add a new paragraph with OpenXML like this:

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

Methods are as followed:

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;
    }
}

and...

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;
    }

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>

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.

Any ideas?

Upvotes: 2

Views: 2472

Answers (1)

kjhughes
kjhughes

Reputation: 111491

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.

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

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.

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

Upvotes: 3

Related Questions