有趣的問題,所以+1。而標記爲已回答,但...
「但是,我無法找到一個方法來做到這一點不添加表到文檔中。」
它是可能的。包裹PdfPTable
在ColumnText
對象,並採取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被使用,但僅用於說明的任何組合/行數將起作用。
非常詳細和有用的答案 – mozillanerd 2012-03-13 16:32:09
感謝上帝我發現了! – 2012-06-06 11:36:56