C# Aspose.Words: 计算固定行高单元格容纳行数
您可以使用 Aspose.Words 中的以下方法来获取固定行高单元格在指定字体及字体大小下,能容纳的行数:
- 首先,创建一个 Document 对象并打开您需要进行操作的文档。
Document doc = new Document('input.docx');
- 获取您需要进行操作的表格,可以使用 Document 对象中的 GetChildNodes 方法来获取所有的表格,然后通过索引获取您需要的表格。
Table table = doc.GetChildNodes(NodeType.Table, true)[0] as Table;
- 获取您需要进行操作的单元格,可以使用 Table 对象中的 Rows 和 Cells 属性来获取相应的单元格。
Cell cell = table.Rows[0].Cells[0];
- 设置相应的字体及字体大小。
Font font = new Font('Arial', 12);
- 使用 Aspose.Words 中的 LayoutEnumerator 对象来获取单元格在指定字体及字体大小下可容纳的行数。
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
enumerator.Current = cell.FirstParagraph;
double cellHeight = cell.CellFormat.Height;
int rowCount = 0;
while (enumerator.MoveNext() && enumerator.Rectangle.Height <= cellHeight)
{
if (enumerator.Rectangle.Bottom > cellHeight)
break;
if (enumerator.Rectangle.Height == 0)
continue;
if (enumerator.Rectangle.Bottom <= cellHeight)
rowCount++;
}
Console.WriteLine('能容纳的行数为:' + rowCount);
- 最后关闭文档。
doc.Save('output.docx');
以上就是使用 Aspose.Words 来获取固定行高单元格在指定字体及字体大小下,能容纳的行数的步骤。
原文地址: https://www.cveoy.top/t/topic/nKcV 著作权归作者所有。请勿转载和采集!