2012-03-13 55 views
3

我想知道是否需要拨打document.NewPage()的第一和第二列高度。但是,我无法找到一种方法来执行此操作,而无需将该表添加到文档中。iTextSharp:如何在表格中找到第一和第二行高度?

实施例:

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

参见my answer here。基本上,你不能知道表格的尺寸,直到它呈现。但是,您可以将表格渲染为您刚丢弃的文档,然后再重新渲染。

+1

非常详细和有用的答案 – mozillanerd 2012-03-13 16:32:09

+1

感谢上帝我发现了! – 2012-06-06 11:36:56

2

有趣的问题,所以+1。而标记为已回答,但...

“但是,我无法找到一个方法来做到这一点不添加表到文档中。”

可能的。包裹PdfPTableColumnText对象,并采取ColumnText.Go()超负荷的优势得到你想要的行的任意数/总高度不添加PdfPTableDocument。下面是一个简单的辅助方法:

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

除非您设置表格的宽度,否则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); 
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); 

如果该行不适合您的条件,你可以简单地从该表中的行集合中删除它。表格的总高度也将被正确计算。

相关问题