C# iTextSharp:如何找到表格中第一行和第二行的高度?
我想获得第一列和第二列的高度,以了解是否需要调用C# iTextSharp:如何找到表格中第一行和第二行的高度?,c#,itext,C#,Itext,我想获得第一列和第二列的高度,以了解是否需要调用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.GetR
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);
PdfPRow firstRow=新的PdfPRow(cells1.ToArray());
table.Rows.Add(第一行);
PdfPRow secondRow=新的PdfPRow(cells2.ToArray());
table.Rows.Add(第二行);
浮点h1=表格.GetRowHeight(0),h2=表格.GetRowHeight(1);
if(当前y-h1-h2<30)document.NewPage();
文件。添加(表);
请参阅。基本上,在表呈现之前,您无法知道表的维度。但是,您可以将表呈现为一个文档,然后将其丢弃,稍后再重新呈现。有趣的问题,所以+1。已经标记为已回答,但是
“但是,如果不将表添加到文档中,我无法找到执行此操作的方法。”
这是可能的。将PdfPTable
包装在ColumnText
对象中,并利用重载获取所需的任意行数的总高度,而无需将PdfPTable
添加到文档中。下面是一个简单的助手方法:
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"));
}
使用(文档=新文档()){
PdfWriter writer=PdfWriter.GetInstance(文档,流);
document.Open();
PdfPTable=新的PdfPTable(4);
对于(int i=1;i<20;++i){
表.AddCell(i.ToString());
}
int[]wantedRows={0,2,3};
文件。添加(新段落)(string.Format(
“模拟表格高度:{0}”,
TotalRowHeights(文档、writer.DirectContent、表格、wantedRows)
)));
//取消下面的注释块,以验证计算的高度是否正确
/*
文件。添加(新段落(“添加PdfPTable”);
文件。添加(表);
浮动总高度=0f;
foreach(整数i,以所需行为单位){
totalHeight+=表.GetRowHeight(i);
}
文件。添加(新段落)(string.Format(
“添加表后的高度:{0}”,totalHeight
)));
*/
文件。添加(新段落(“试验段落”);
}
在用例中,行1、3和4被使用,但仅用于演示任何行的组合/数量是否有效。除非您设置表格的宽度,否则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);
//添加
表1.TotalWidth=400f;
//
PdfPRow firstRow=新的PdfPRow(cells1.ToArray());
table.Rows.Add(第一行);
PdfPRow secondRow=新的PdfPRow(cells2.ToArray());
table.Rows.Add(第二行);
浮点h1=表格.GetRowHeight(0),h2=表格.GetRowHeight(1);
if(当前y-h1-h2<30)document.NewPage();
文件。添加(表);
还有另一种方法:
首先创建表
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);
如果该行不符合您的条件,只需将其从表的rows集合中删除即可。桌子的总高度也将被正确计算。非常详细,回答非常有用