向 PdfPTable、iTextSharp 添加列

发布于 2024-08-23 11:12:34 字数 153 浏览 8 评论 0原文

我正在使用 iTextSharp 创建 PDF 文档。我看到如何创建一个包含多个列的新表,但无论如何我看不到动态添加新列。我遇到的问题是我不会立即知道我需要的列数,因此需要继续添加它们

有人可以启发我吗?或者我每次需要添加时都必须重新创建表一个专栏?

谢谢 垫

I'm creating a PDF document using iTextSharp. I see how to create a new table with a number of columns but I can't see anyway to dynamically add a new column. The problem I have is I'm not going to know the number of columns I need straight away, so need to keep adding them

Can somebody please enlighten me or am I going to have to re-create the table each time I need to add a column?

Thanks
Mat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你只要分分秒秒 2024-08-30 11:12:34

那么创建一个包含您想要的表格并且能够为您创建 PDF 表格的中间模型不是很有意义吗?

我知道这听起来像是很多工作,但从长远来看,它应该会有所帮助,因为您将能够在构建行和列时动态更改行和列,然后在最后,只需“编译”表并吐出输出 PdfPTable 对象?

Wouldn't it then make sense to create an intermediate model which contains the table you want, and is capable of then creating the PDF table for you?

I know it sounds like a lot of work, but in the long run it should help in that you would be able to dynamically alter the rows and columns as you build them, and then at the end, simply "compile" the table and spit out the PdfPTable object?

烟若柳尘 2024-08-30 11:12:34

PdfPTable 表在创建后就列数而言是不可变的。

我能想到的唯一解决方法是从一大堆列开始......不,这也行不通。您甚至无法将单元格添加到现有行。我想你可以尝试使用列跨越来掩盖额外的列,并在向行添加更多单元格时调整它们,但这也行不通。

添加列时必须重建表。没有办法解决它。

我强烈建议您在首先创建表之前弄清楚如何确定列数......即使您必须“试运行”数据。使用某种中间格式(String[][] 或其他格式)来保存数据,然后从中构建表,而不是提供给您的数据。或者至少跟踪您需要多少列。

考虑到大量数据,单次传递可能不切实际/不可能。但是多次重建整个表再好不过了。这确实是一个性能调优问题,只有您有信息可以回答。

PdfPTable tables are immutable as far as column count goes once created.

The only workaround I can think of is to start with a Whole Bunch of columns and... nope that won't work either. You cannot even add cells to an existing row. I was thinking you could play around with the column spanning to mask your extra columns and adjust them as you added more cells to the rows, but that won't work either.

You must rebuild the table when adding columns. No way around it.

I strongly recommend you figure out how to determine your column count before creating the table in the first place... even if you have to "dry run" through your data. Use some intermediate format (String[][] or whatever) to keep your data, then build the table from that, not the data as it comes to you. Or at least track how many columns you'll need.

Given a huge amount of data, a single pass may not be practical/possible. But rebuilding your whole table several times can't be much better. That's really a performance tuning question that only you have the information to answer.

(り薆情海 2024-08-30 11:12:34

ITextSharp 表的工作方式与 HTML 表不同(我猜您已经习惯了)。

您需要告诉它的是您拥有的列数,然后继续添加单元格。

假设您创建一个包含 5 列的 pdfptable。您添加的第 5 个单元格将位于第一行,第 6 个单元格将位于第二行的第一列。

唯一的缺点是,如果您需要添加并非填充所有单元格的行,但我通常通过添加一个空单元格或其中包含空格的单元格来解决这个问题。

ITextSharp tables work in a different way to HTML tables (which I guess you're used to).

All you need to tell it is the number of columns you have and then keep adding cells.

Say you create a pdfptable with 5 columns. The 5th cell you add will be on the first row and the 6th cell will be in the 1st column on your 2nd row.

The only downside to this is if you need to add rows where not all the cells are populated but I usually get around this by just adding an empty cell or a cell with a space in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文