如何在iTextSharp中设置PdfPTable的高度 [英] How to Set Height of PdfPTable in iTextSharp

查看:1881
本文介绍了如何在iTextSharp中设置PdfPTable的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了iTextSharp dll的最新版本。我生成了一个PdfPTable对象,我必须设置它的高度。尽管设置了PdfPTable的宽度,但我无法设置它的高度。一些作者建议使用'setFixedHeight'方法。但最后一个版本的iTextSharp.dll没有'setFixedHeight'方法。它的版本是5.5.2。我该怎么办?

i downloaded the last version of iTextSharp dll. I generated a PdfPTable object and i have to set it's height. Despite to set width of PdfPTable, im not able to set height of it. Some authors suggest to use 'setFixedHeight' method. But the last version of iTextSharp.dll has not method as 'setFixedHeight'. It's version is 5.5.2. How can i do it?

推荐答案

一旦你开始考虑它,设置一个表的高度是没有意义的。或者,它是有道理的,但留下许多未回答或无法回答的问题。例如,如果您将两行表格设置为500的高度,这是否意味着每个单元格的高度为250?如果大图像放在第一行,如果表格通过拆分400/100自动响应怎么办?那么两行中的大内容怎么样呢?它应该压扁那些吗?这些场景中的每一个都会产生不同的结果,从而使得知道表格实际上不可靠。如果您查看 HTML规范,您会看到他们不知道甚至不允许为表格设置固定的高度。

Setting a table's height doesn't make sense once you start thinking about it. Or, it makes sense but leaves many questions unanswered or unanswerable. For instance, if you set a two row table to a height of 500, does that mean that each cell get's 250 for a height? What if a large image gets put in the first row, should the table automatically respond by splitting 400/100? Then what about large content in both rows, should it squish those? Each of these scenarios produces different results that make knowing what a table will actually do unreliable. If you look at the HTML spec you'll see that they don't even allow setting a fixed height for tables.

然而,有一个简单的解决方案,它只是设置单元格本身的固定高度。只要您不使用 new PdfPCell(),您只需将 DefaultCell.FixedHeight 设置为您想要的任何内容。

However, there's a simple solution and that's just setting the fixed height of the cells themselves. As long as you aren't using new PdfPCell() you can just set DefaultCell.FixedHeight to whatever you want.

var t = new PdfPTable(2);
t.DefaultCell.FixedHeight = 100f;

t.AddCell("Hello");
t.AddCell("World");
t.AddCell("Hello");
t.AddCell("World");

doc.Add(t);

如果您手动创建单元格,则需要设置 FixedHeight

If you are manually creating cells then you need to set the FixedHeight on each:

var t = new PdfPTable(2);

for(var i=0;i<4;i++){
    var c = new PdfPCell(new Phrase("Hello"));
    c.FixedHeight = 75f;
    t.AddCell(c);
}

doc.Add(t);

但是,如果你想要正常的桌子,必须设置一个固定的高度,以便切掉那些不合适的东西。适合你也可以使用 ColumnText 。我不推荐这个,但你可能有一个案例。下面的代码只显示六行。

However, if you want normal table-ness and must set a fixed height that chops things that don't fit you can also use a ColumnText. I wouldn't recommend this but you might have a case for it. The code below will only show six rows.

var ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(100, 100, 200, 200);

var t = new PdfPTable(2);
for(var i=0;i<100;i++){
    t.AddCell(i.ToString());
}
ct.AddElement(t);
ct.Go();

这篇关于如何在iTextSharp中设置PdfPTable的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆