0

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 |
+---+---+---+

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 |
+---+---+---+

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.

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?

Betty Crokker
  • 3,001
  • 6
  • 34
  • 68
  • That would only work if you add the content `A`, `K` and `G` using cell events instead of adding it as "real" content of the cell. – Bruno Lowagie Sep 23 '15 at 20:00
  • I see http://stackoverflow.com/questions/23956377/can-i-use-an-itextsharp-cell-event-to-repeat-data-on-the-next-page-when-a-row-is as an example, but it looks like it only gives me the chance to control the drawing of the text, not the drawing of the borders? – Betty Crokker Sep 23 '15 at 20:09
  • (1.) Why do you need to draw the borders in the cell event? The borders are drawn automatically (unless you use `setBorder(PdfPCell.NO_BORDER)`). (2.) What makes you think you can't draw borders in a cell event? Did you read [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). There's a reference to [this question](http://stackoverflow.com/questions/23650957/how-to-create-a-rounded-corner-table-using-itext-itextsharp). – Bruno Lowagie Sep 23 '15 at 20:19
  • I'm making progress but have a related question: http://stackoverflow.com/questions/32749219/itextsharp-text-is-different-when-drawn-with-cell-event-vs-directly – Betty Crokker Sep 23 '15 at 20:49
  • Bruno - you are right, the answer is to create cell events. Do you want to add that as an answer so I can mark it as the right answer? – Betty Crokker Sep 23 '15 at 21:51
  • Sure! Will do that right away. – Bruno Lowagie Sep 24 '15 at 12:47

1 Answers1

1

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.

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);

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);

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.

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?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I found an interesting solution ... I do the rowspan and create the cell with the phrase as normal. That gives me all the right borders, and the phrase is drawn in the correct place the first time - just not on subsequent pages. But if I then add a cell event that draws the phrase, its CellLayout() gets called at the top of each new page! This is undoubtedly undocumented behavior and I should never upgrade iTextSharp! – Betty Crokker Sep 24 '15 at 14:18
  • Just make sure that you don't write the content twice on the first page. In the old days (I'm talking about the early years of PDF, starting in 1993), **bold fonts** didn't exist on all systems. **Bold** was mimicked by writing the same content multiple times at the same location. Back then, viewers knew that they should write text in bold when this happened. Modern viewers have forgotten this practice, but some still apply it for backward compatibility. Your approach may result in the text on the first page to appear as if the font was bold. – Bruno Lowagie Sep 24 '15 at 14:48
  • I wouldn't say the behavior is undocumented. It's known as one of the possible use cases for cell events. – Bruno Lowagie Sep 24 '15 at 14:49
  • I'm glad it's not undocumented behavior! You are right about writing content twice, although the result is not two phrases written almost on top of each other, it's two phrases, one after the other. What I did is this: in my class that implements IPdfPCellEvent, I have a member variable that keeps track of whether it's the first time CellLayout() is called or not. The first time I do nothing (that's the time the standard code is writing the phrase), it's only on subsequent calls to CellLayout() that I write the phrase. – Betty Crokker Sep 24 '15 at 16:02
  • That's indeed the way to work around this problem. Well done! My comment about writing text twice on top of each other was just a "did you know" comment about the history of PDF :D – Bruno Lowagie Sep 24 '15 at 16:05
  • Can you please look into this Query of mine? https://stackoverflow.com/questions/70921938/how-to-achieve-multi-header-in-itext – Lucifer Geralt Jan 31 '22 at 13:45