从断裂超过页面防止表,如果可能的话 [英] Prevent tables from breaking over a page, if possible

查看:171
本文介绍了从断裂超过页面防止表,如果可能的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的OpenXML生成包含数千个表的Word文档。他们中的一些跨越整个页面,这是细的长度,因为没有办法避免这种情况发生,然而,许多表包含只有几行。有没有我可以设置为阻止这些表破裂的属性?

I am using OpenXML to generate a word document which contains thousands of tables. Some of them span over the length of entire page, which is fine as there is no way to prevent that from happening, however, many of the tables contain only a few rows. Is there a property I can set to prevent these tables from breaking?

它看起来可怕,当只有两个行的表是两页之间的分裂,或者当一个键行是以前的页面上唯一的分裂。我花了很多时间认真块导航,没有运气的MSDN网页...希望有人已经这样做过。

It looks awful when a table with only two rows is split between two pages, or when a key row is the only one split on the previous page. I've spent a serious chunk of time navigating the MSDN pages with no luck... hopefully somebody has done this before.

推荐答案

这是一种痛苦,但有一个办法做到这一点。如果您正在编辑直接在Word文档,想要实现这个行为,你会下段>行和分页保持与下一个和保持线条一起性质起。基本上,你会检查框启用此属性为表中的每一行(技术上所有,但最后一排,虽然设置所有行可能工作以及)。因为任何可以在Word中进行可以使用OOXML API以及完成的,它只是想出来的问题。下面是说明所需的代码的代码片段。

This is kind of a pain, but there is a way to do it. If you were editing a document directly in Word and wanted to achieve this behavior, you would play with the "Keep with next" and "Keep lines together" properties under Paragraph > Line and Page Breaks. Basically you would check the box to enable this property for each row in the table (technically all but the last row, although setting all rows may work as well). Since anything that can be done in Word can be accomplished using the OOXML API as well, it's just a matter of figuring it out. Here's a code snippet that illustrates the code needed.

首先,下面的代码生成的Word文档与表格跨页将休息。它首先增加了一个简单的样式表,然后添加一系列段落推表页面向下,最后才在底部添加表。

First, the code below generates a Word document with a table that WILL break across pages. It first adds a simple style for the table, then adds a series of paragraphs to push the table down the page, before finally adding the table at the bottom.

using (WordprocessingDocument document = WordprocessingDocument.Create("TableBreaksAcrossPage.docx", WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();

            #region Styles

            Styles styles = new Styles();

            Style style = new Style() { Type = StyleValues.Table, StyleId = "TableGrid" };
            StyleName styleName10 = new StyleName() { Val = "Table Grid" };
            BasedOn basedOn2 = new BasedOn() { Val = "TableNormal" };
            UIPriority uIPriority8 = new UIPriority() { Val = 59 };
            Rsid rsid7 = new Rsid() { Val = "003B7411" };

            StyleParagraphProperties styleParagraphProperties2 = new StyleParagraphProperties();
            SpacingBetweenLines spacingBetweenLines4 = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

            styleParagraphProperties2.Append(spacingBetweenLines4);

            StyleTableProperties styleTableProperties4 = new StyleTableProperties();
            TableIndentation tableIndentation4 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa };

            TableBorders tableBorders2 = new TableBorders();
            TopBorder topBorder2 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            LeftBorder leftBorder2 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideHorizontalBorder insideHorizontalBorder2 = new InsideHorizontalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideVerticalBorder insideVerticalBorder2 = new InsideVerticalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };

            tableBorders2.Append(topBorder2);
            tableBorders2.Append(leftBorder2);
            tableBorders2.Append(bottomBorder2);
            tableBorders2.Append(rightBorder2);
            tableBorders2.Append(insideHorizontalBorder2);
            tableBorders2.Append(insideVerticalBorder2);

            TableCellMarginDefault tableCellMarginDefault4 = new TableCellMarginDefault();
            TopMargin topMargin4 = new TopMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellLeftMargin tableCellLeftMargin4 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa };
            BottomMargin bottomMargin4 = new BottomMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellRightMargin tableCellRightMargin4 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa };

            tableCellMarginDefault4.Append(topMargin4);
            tableCellMarginDefault4.Append(tableCellLeftMargin4);
            tableCellMarginDefault4.Append(bottomMargin4);
            tableCellMarginDefault4.Append(tableCellRightMargin4);

            styleTableProperties4.Append(tableIndentation4);
            styleTableProperties4.Append(tableBorders2);
            styleTableProperties4.Append(tableCellMarginDefault4);

            style.Append(styleName10);
            style.Append(basedOn2);
            style.Append(uIPriority8);
            style.Append(rsid7);
            style.Append(styleParagraphProperties2);
            style.Append(styleTableProperties4);

            styles.Append(style);

            StyleDefinitionsPart styleDefinitionsPart = mainDocumentPart.AddNewPart<StyleDefinitionsPart>("Styles");
            styleDefinitionsPart.Styles = styles;

            #endregion

            mainDocumentPart.Document =
            new Document(
                new Body(
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Table(
                        new TableProperties(
                            new TableStyle() { Val = "TableGrid" },
                            new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }),
                        new TableGrid(
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" }),
                        new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1"))))),
                       new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2"))))),
                      new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3"))))),
                     new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4"))))))));
        }



下面的代码生成与表中的Word文档不会跨页断。需要注意的是每个段落的ParagraphProperties有KeepNext()和KeepLines()附加类的实例。据我所知,这需要为每个TableCell的,这是痛苦的部分来完成。也许能够创造细胞卸载到一个辅助方法,虽然,以避免所有重复的代码。运行代码,看看自己,希望它帮助。

The code below generates a Word document with a table that WILL NOT break across pages. Note that the ParagraphProperties of each paragraph have an instance of the KeepNext() and KeepLines() classes appended. As far as I can tell, this needs to be done for each TableCell, which is the painful part. Might be able to offload cell creation to a helper method though, to avoid all the repetitive code. Run the code and see for yourself, hope it helps.

using (WordprocessingDocument document = WordprocessingDocument.Create("TableDoesNotBreakAcrossPage.docx", WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();

            #region Styles

            Styles styles = new Styles();

            Style style = new Style() { Type = StyleValues.Table, StyleId = "TableGrid" };
            StyleName styleName10 = new StyleName() { Val = "Table Grid" };
            BasedOn basedOn2 = new BasedOn() { Val = "TableNormal" };
            UIPriority uIPriority8 = new UIPriority() { Val = 59 };
            Rsid rsid7 = new Rsid() { Val = "003B7411" };

            StyleParagraphProperties styleParagraphProperties2 = new StyleParagraphProperties();
            SpacingBetweenLines spacingBetweenLines4 = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

            styleParagraphProperties2.Append(spacingBetweenLines4);

            StyleTableProperties styleTableProperties4 = new StyleTableProperties();
            TableIndentation tableIndentation4 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa };

            TableBorders tableBorders2 = new TableBorders();
            TopBorder topBorder2 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            LeftBorder leftBorder2 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideHorizontalBorder insideHorizontalBorder2 = new InsideHorizontalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideVerticalBorder insideVerticalBorder2 = new InsideVerticalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };

            tableBorders2.Append(topBorder2);
            tableBorders2.Append(leftBorder2);
            tableBorders2.Append(bottomBorder2);
            tableBorders2.Append(rightBorder2);
            tableBorders2.Append(insideHorizontalBorder2);
            tableBorders2.Append(insideVerticalBorder2);

            TableCellMarginDefault tableCellMarginDefault4 = new TableCellMarginDefault();
            TopMargin topMargin4 = new TopMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellLeftMargin tableCellLeftMargin4 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa };
            BottomMargin bottomMargin4 = new BottomMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellRightMargin tableCellRightMargin4 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa };

            tableCellMarginDefault4.Append(topMargin4);
            tableCellMarginDefault4.Append(tableCellLeftMargin4);
            tableCellMarginDefault4.Append(bottomMargin4);
            tableCellMarginDefault4.Append(tableCellRightMargin4);

            styleTableProperties4.Append(tableIndentation4);
            styleTableProperties4.Append(tableBorders2);
            styleTableProperties4.Append(tableCellMarginDefault4);

            style.Append(styleName10);
            style.Append(basedOn2);
            style.Append(uIPriority8);
            style.Append(rsid7);
            style.Append(styleParagraphProperties2);
            style.Append(styleTableProperties4);

            styles.Append(style);

            StyleDefinitionsPart styleDefinitionsPart = mainDocumentPart.AddNewPart<StyleDefinitionsPart>("Styles");
            styleDefinitionsPart.Styles = styles;

            #endregion

            mainDocumentPart.Document =
            new Document(
                new Body(
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Table(
                        new TableProperties(
                            new TableStyle() { Val = "TableGrid" },
                            new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }),
                        new TableGrid(
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" }),
                        new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1"))))),
                       new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2"))))),
                      new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3"))))),
                     new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4"))))))));
        }

这篇关于从断裂超过页面防止表,如果可能的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆