iText(夏普):带有标题和子标题的表格 [英] iText(Sharp): tables with headers and subheaders

查看:131
本文介绍了iText(夏普):带有标题和子标题的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText(iTextSharp版本5.5.7),我正在创建一个PdfPTable,其中行中的数据被排序。举一个具体的例子,假设我的数据看起来像这样(包括我的标题 - h1,h2和h3):

I'm using iText (iTextSharp version 5.5.7) and I am creating a PdfPTable where the data in the rows is sorted. To give a specific example, say my data looks like this (including my headers - h1, h2, and h3):

+---+---+---+
|h1 |h2 |h3 |
+---+---+---+
| A | B | C |
+---+---+---+
| A | B | D |
+---+---+---+
| A | E | F |
+---+---+---+
| K | G | H |
+---+---+---+
| K | G | I |
+---+---+---+
| K | G | J |
+---+---+---+

我有工作,然后我开始设置PdfPCell的Rowspan属性,这样我就可以避免打印重复的文本。这也很有效,我得到的是:

I've got that working, and then I started setting the Rowspan property of PdfPCell so I can avoid printing repeated text. That's also working great, what I get is this:

+---+---+---+
|h1 |h2 |h3 |
+---+---+---+
| A | B | C |
|   |   +---+
|   |   | D |
|   +---+---+
|   | E | F |
+---+---+---+
| K | G | H |
|   |   +---+
|   |   | I |
|   |   +---+
|   |   | J |
+---+---+---+

问题是,我点击分页符,我看到的是:

The problem is, I hit page breaks, and what I see is this:

+---+---+---+
|h1 |h2 |h3 |
+---+---+---+
| A | B | C |
|   |   +---+
|   |   | D |
|   +---+---+
|   | E | F |
+---+---+---+
| K | G | H |
+---+---+---+

Page Break

+---+---+---+
|h1 |h2 |h3 |
+---+---+---+
|   |   | I |
|   |   +---+
|   |   | J |
+---+---+---+

我想要什么,当第二页开始时,我希望重新打印跨区单元格(在本例中为K和G),以便用户知道发生了什么。

What I want, is that when that second page starts, I want the spanned cells (in this case 'K' and 'G') to be re-printed so the user has some idea what's going on.

我需要的是类似于HeaderRow,但我需要在发出行时更改标题行。

What I need is similar to a HeaderRow, but what I need the header row to be changes as the rows are emitted.

关于如何进行的任何想法使这个工作吗?

Any ideas on how to make this work?

推荐答案

您可以为 PdfPTable定义页眉(和页脚)行,但这不会解决您的问题,因为这些页眉(或页脚)行重复一个完整的行,而您只想重复一部分行。

You can define header (and footer) rows for PdfPTable, but that won't solve your problem as these header (or footer) rows repeat a complete row whereas you only want to repeat part of a row.

这并不意味着您的要求无法满足。您可以通过在单元格事件中添加内容而不是直接将其添加到单元格来解决此问题。

This doesn't mean your requirement can't be met. You can work around the problem by adding the content in a cell event instead of adding it directly to a cell.

例如:您当前添加的内容如 A B K G 像这样:

For instance: you currently add content such as A, B, K and G like this:

PdfPCell cell = new PdfPCell(new Phrase("A"));
cell.setRowspan(3);
table.addCell(cell);

如果此单元格被拆分并分布在多个页面上,则内容A 只会出现在第一页上。后续页面上不会有任何内容。

If this cell is split and distributed over multiple pages, the content "A" will only appear on the first page. There won't be any content on the subsequent pages.

您可以通过添加单元来解决此问题,您可以为其定义单元格事件:

You can solve this by adding an empty cell for which you define a cell event:

PdfPCell cell = new PdfPCell();
cell.setCellEvent(new MyCellEvent("A"));
cell.setRowspan(3);
table.addCell(cell);

您现在必须编写 PdfPCellEvent的实现以添加内容的方式接口并实现 cellLayout 方法(在本例中为A)使用传递给此方法的坐标作为参数。

You now have to write an implementation of the PdfPCellEvent interface and implement the cellLayout method in such a way that adds the content (in this case "A") using the coordinates passed to this method as a parameter.

有关灵感(以及如何使我的Java代码适应.NET的想法),请参阅Can当一行被拆分时,我使用iTextSharp单元格事件在下一页上重复数据?

For inspiration (and an idea on how to adapt my Java code to .NET), see Can I use an iTextSharp cell event to repeat data on the next page when a row is split?

这篇关于iText(夏普):带有标题和子标题的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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