2012-03-13 51 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); 

如果該行不適合您的條件,你可以簡單地從該表中的行集合中刪除它。表格的總高度也將被正確計算。

相關問題