有趣的问题,所以+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