Gembox文档防止表中的分页



在Gembox.Document中,是否可以在文档中插入一个表,防止它被分页符分割,但如果不适合上一页,则将其移动到下一页?我看过样品和文件,但没有发现任何东西。

对于位于该表中的段落,需要将KeepLinesTogetherKeepWithNext属性设置为true。例如,尝试以下操作:

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

编辑
以上内容适用于大多数情况,但是,当Table元素具有空的TableCell元素而不具有任何Paragraph元素时,可能会出现问题。

为此,我们需要在这些TableCell元素中添加一个空的Paragraph元素,以便设置所需的格式(来源:保持表在同一页上):

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

最新更新