问题
I am making an OpenXML document generator. It generates Word Documents based on system data. I Have table headers set up and they work a treat. My issue is as follows: In particular circumstances (if the header fits on the page, but the next row doesn't) it will show the header, then there will be the page break and it will show again on the new page. This is of course expected behavior, and is fixed by manually going into the generated document and adding a new line or two before the table, shifting it to the next page, but I was wondering if there was a way to make the header only show if at least the first cell fits onto the page? We have hundreds of tables being shown on our document and I dislike not having control programatically over how the output will be shown. Any tips or tricks you could recommend?
回答1:
Word has a number of tools to control where/how a page break may (or may not) occur in its automatic text flow:
- I'm assuming you're already aware of the table property to not allow rows to break across pages (all content of a row is forced to be on the same page, no break within the row):
<w:trPr><w:cantSplit/></w:trPr>
in the Word Open XML, corresponding to theCantSplit
class (child of TableRowProperties) in the Open XML SDK /AllowBreakAcrossPages
in the object model / Table Properties/Row/Allow Break across pages in the Word UI. - Then there are properties that work on the paragraphs (
<w:p>
) within the tables cells. Most relevant in this case would be the one that keeps the paragraph that has this property on the same page with the paragraph that follows:<w:pPr><w:keepNext/></w:pPr>
in Word Open XML / theKeepNext
class (child of ParagraphProperties) in the Open XML SDK /Paragraph.KeepWithNext
in the object model / Paragraph/Line and Page breaks/Keep with Next in the Word UI. - There's also a property to force all lines of a paragraph to stay together on a page:
<w:pPr><w:keepLines></w:pPr>
in Word Open XML / theKeepLines
class in the Open XML SDK /Paragraph.KeepTogether
in the object model / *Paragraph/Line and Page breaks/Keep lines together` in the Word UI. - What can also affect how paragraph content breaks is widow/orphan control, that prevents single lines of multi-line paragraphs from ending/starting on a page. This is the only one of these properties that's activated by default:
<w:pPr><w:widowControl/></w:pPr>
in Word Open XML / theWidowControl
class in the Open XML SDK /Paragraph.WidowControl
in the object model / Paragarph/Line and Page breaks / Widow/Orphan control in the Word UI. - Finally, in order to force a page break without actually inserting a page break character, there's a property that can be applied to a paragraph to force it to start on a new page. This is particularly useful as part of a style definition if a new "chapter" should always start on a new page:
<w:pPr><w:pageBreakBefore/></w:pPr>
/PageBreakBefore
class in the Open XML SDK /Paragraph.PageBreakBefore
in the object model / Paragarph/Line and Page breaks/Page break before in the Word UI.
Each of these, individually, is interesting and powerful. They're particularly effective as part of style definitions. In combination they can give you a fairly granular control of page layout, but you do have to be careful. For example, if you simply apply "keep with next" to all paragraph in the table the end effect will be that Word will try to keep the entire table on one page and, if it's longer, will break it as if the property had not been applied at all.
That's one reason I spelled out all the variations of where the commands can be found. It usually helps tremendously to try these out in the UI; or, if you run into problems, to investigate what settings are active in the result that's not quite what you want.
来源:https://stackoverflow.com/questions/36439159/open-xml-table-header-same-page