iTextSharp:如何在表格中找到第一行和第二行的高度?

3

我希望获取第一列和第二列的高度,以便知道是否需要调用document.NewPage()。但是,如果不将表格添加到文档中,我找不到实现这一点的方法。

例如:

PdfPRow firstRow = new PdfPRow(cells1.ToArray());
table.Rows.Add(firstRow);
PdfPRow secondRow = new PdfPRow(cells2.ToArray());
table.Rows.Add(secondRow);

float h1 = table.GetRowHeight(0), h2 = table.GetRowHeight(1);

if (currentY - h1 - h2 < 30) document.NewPage();

document.Add(table);
4个回答

4

请参考这里的答案。基本上,直到表格渲染完成之前你无法知道其尺寸。但是,你可以将表格渲染到一个丢弃的文档中,然后稍后重新渲染它。


1
非常详细和有帮助的答案。 - mozillanerd

2
有趣的问题,+1。已标记为已回答,但是...
"But, I can't find a way to do this without adding the table to the document."
这是可能的。将“PdfPTable”包装在“ColumnText”对象中,并利用{{link1:ColumnText.Go()}}重载以获取任意/所需行数的总高度,而无需将“ PdfPTable”添加到“Document”中。以下是一个简单的帮助程序方法:
public static float TotalRowHeights(
  Document document, PdfContentByte content, 
  PdfPTable table, params int[] wantedRows) 
{
  float height = 0f;
  ColumnText ct = new ColumnText(content);
// respect current Document.PageSize    
  ct.SetSimpleColumn(
    document.Left, document.Bottom, 
    document.Right, document.Top
  );
  ct.AddElement(table);
// **simulate** adding the PdfPTable to calculate total height
  ct.Go(true);
  foreach (int i in wantedRows) {
    height += table.GetRowHeight(i);
  }
  return height;
}

一个在5.2.0.0版本下测试过的简单用例:
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  PdfPTable table = new PdfPTable(4);
  for (int i = 1; i < 20; ++i) {
    table.AddCell(i.ToString());
  }
  int[] wantedRows = {0, 2, 3};
  document.Add(new Paragraph(string.Format(
    "Simulated table height: {0}",
    TotalRowHeights(document, writer.DirectContent, table, wantedRows)
  )));
// uncomment block below to verify correct height is being calculated
/* 
  document.Add(new Paragraph("Add the PdfPTable"));
  document.Add(table);
  float totalHeight = 0f;
  foreach (int i in wantedRows) {
    totalHeight += table.GetRowHeight(i);
  }
  document.Add(new Paragraph(string.Format(
    "Height after adding table: {0}", totalHeight
  )));
*/
  document.Add(new Paragraph("Test paragraph"));
}

在这个使用案例中,第1、3、4行被使用了,但仅是为了展示任意组合/数量的行都可以工作。

2

还有另一种方法可以实现这个功能:首先创建表格。

    this.table = new PdfPTable(relativeColumnWidths);
    this.table.SetTotalWidth(absoluteColumnWidths);
    this.rowCells.Clear();

您现在可以使用表格单元格填充列表:

    Paragraph pText = new Paragraph(text, this.font);
    PdfPCell cell = new PdfPCell(pText);
    this.rowCells.Add(cell);

当您准备创建新行时:

    PdfPRow row = new PdfPRow(this.rowCells.ToArray());
    this.table.Rows.Add(row);

这并没有什么特别的。但是如果你现在重新设置表格宽度,你就能够正确地计算行高:

    this.table.SetTotalWidth(this.table.AbsoluteWidths);
    this.rowCells.Clear();
    float newRowsHeight = this.table.GetRowHeight(this.table.Rows.Count - 1);

如果行不符合您的条件,您可以从表格的行集合中将其删除。表格的总高度也将被正确计算。

2
除非您设置表格的宽度,否则table.GetRowHeight(0)将始终返回零。
// added
table.TotalWidth = 400f;     
//
PdfPRow firstRow = new PdfPRow(cells1.ToArray());

table.Rows.Add(firstRow);
PdfPRow secondRow = new PdfPRow(cells2.ToArray());
table.Rows.Add(secondRow);

float h1 = table.GetRowHeight(0), h2 = table.GetRowHeight(1);

if (currentY - h1 - h2 < 30) document.NewPage();

document.Add(table);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接