将表(行)与 OpenXML SDK 2.5 保持在一起 [英] keep table(rows) together with OpenXML SDK 2.5

查看:25
本文介绍了将表(行)与 OpenXML SDK 2.5 保持在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 word 文档中生成多个表,每行 2 行.但我想将这两行放在一起(如果可能的话).

I'd like to generate multiple tables with 2 rows each inside of a word document. But I want to keep these two rows together (if possible).

  1. new KeepNext()在第一行不起作用
  2. new KeepNext()在第一行的最后一段不起作用
  3. new CantSplit() 在桌子上不起作用
  1. new KeepNext()on the first row doesn't work
  2. new KeepNext()on the last paragraph of the first row doesn't work
  3. new CantSplit() on the table doesn't work

在所有情况下,第二行(如果太大)都落在第二页上.最后一个单元格(内容较大的单元格)上的 new CantSplit() 避免了单元格的中断.但是没有一个选项可以避免表格的拆分(按行).

In all cases the second row (if too large) lands on the second page. A new CantSplit() on the last cell ( the one with large content) avoids a break of the cell. But none of the options avoids a split of the table (rowwise).

推荐答案

您需要为 每一 行添加一个 KeepNext 以将它们保持在一起.document.xml 中的 XML 输出应该类似于:

You need to add a KeepNext to each row to keep them together. The XML output in document.xml should be something akin to:

此代码成功创建了一个包含 2 行且跨页面保持在一起的表格:

This code successfully creates a table with 2 rows that will stay together across pages:

Table table = wordDoc.MainDocumentPart.Document.Body.AppendChild(new Table());

TableRow row1 = table.AppendChild(new TableRow());
TableCell cell1 = row1.AppendChild(new TableCell());
Paragraph para1 = cell1.AppendChild(new Paragraph());
PreviousParagraphProperties prop1 = para1.AppendChild(new PreviousParagraphProperties());
KeepNext k = prop1.AppendChild(new KeepNext());
Run run1 = para1.AppendChild(new Run());
run1.AppendChild(new Text("This is some long text"));

TableRow row2 = table.AppendChild(new TableRow());
TableCell cell2 = row2.AppendChild(new TableCell());
Paragraph para2 = cell2.AppendChild(new Paragraph());
PreviousParagraphProperties prop2 = para1.AppendChild(new PreviousParagraphProperties());
KeepNext k2 = prop2.AppendChild(new KeepNext());
Run run2 = para2.AppendChild(new Run());
run2.AppendChild(new Text("This is some even longer text"));

这篇关于将表(行)与 OpenXML SDK 2.5 保持在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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