简体   繁体   English

Gembox Document防止表格分页?

[英]Gembox Document prevent pagebreak in table?

Is it possible in Gembox.Document to insert a table to a document and prevent it from being split by a page break but instead move it to the next page if it does not fit on the previous page? 是否可以在Gembox.Document中将表格插入文档中,并防止其被分页符拆分,但如果它不适合上一页,则将其移至下一页? I have looked through the samples and the documentation but not found anything. 我浏览了样本和文档,但没有发现任何东西。

You need to set KeepLinesTogether and KeepWithNext properties to true for paragraphs that are located in that table. 您需要将位于该表中的段落的KeepLinesTogetherKeepWithNext属性设置为true For example try the following: 例如,尝试以下操作:

Table table = ...

foreach (ParagraphFormat paragraphFormat in table
    .GetChildElements(true, ElementType.Paragraph)
    .Cast<Paragraph>()
    .Select(p => p.ParagraphFormat))
{
    paragraphFormat.KeepLinesTogether = true;
    paragraphFormat.KeepWithNext = true;
}

EDIT 编辑
The above will work for most cases, however the problem can occur when the Table element has empty TableCell elements, that do not have any Paragraph elements. 上面的方法适用于大多数情况,但是当Table元素具有空的TableCell元素(没有任何Paragraph元素)时,可能会发生此问题。

For this we need to add an empty Paragraph element to those TableCell elements so that we can set the desired formatting (source: Keep Table on same page ): 为此,我们需要向这些TableCell元素添加一个空的Paragraph元素,以便我们可以设置所需的格式(来源: 在同一页面上保留Table ):

// Get all Paragraph formats in a Table element.
IEnumerable<ParagraphFormat> formats = table
    .GetChildElements(true, ElementType.TableCell)
    .Cast<TableCell>()
    .SelectMany(cell =>
    {
        if (cell.Blocks.Count == 0)
            cell.Blocks.Add(new Paragraph(cell.Document));
        return cell.GetChildElements(true, ElementType.Paragraph);
    })
    .Cast<Paragraph>()
    .Select(p => p.ParagraphFormat);

// Set KeepLinesTogether and KeepWithNext properties.
foreach (ParagraphFormat format in formats)
{
    format.KeepLinesTogether = true;
    format.KeepWithNext = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM