Reputation: 1025
I have created a PdfPTable (with a header) that is split over several pages. I use ColumnText in simulation mode to add the table and calculate the number of pages the table is shown on. Now I'm looking for the best way to add a x/n counter for each header.
Example: my header line is "Table about stuff". I calculate, that the table is split across 3 pages. The resulting headers should look like: "Table about stuff 1/3" for page one, "Table about stuff 2/3" for page two, "Table about stuff 3/3" for page three.
What would be the best way to do this? Is it even possible?
Upvotes: 0
Views: 1022
Reputation: 77528
The best way to do this would be to use a cell event.
I don't have a ready made example, but you could compose one yourself by combining the following examples:
In the MovieCountries1 example, I solved the "page X of Y" problem. You always know the value of X, but as long as you're still creating the document, you don't know the value of Y (which is the total number of pages). In your case, you have a similar problem. You want to add "part X of Y".
You can achieve this by adding an emptry cell for which you create a custom PdfPCellEvent
. You can find several examples with cell events by looking for the keywords PdfPCell > events on the official iText site.
Let's take a look at the RunLengthEvent example where we use a custom PdfPCellEvent
named PressPreview
:
class PressPreview implements PdfPCellEvent {
public BaseFont bf;
public PressPreview() throws DocumentException, IOException {
bf = BaseFont.createFont();
}
public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] canvas) {
PdfContentByte cb = canvas[PdfPTable.TEXTCANVAS];
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(Element.ALIGN_RIGHT, "PRESS PREVIEW",
rect.getRight() - 3, rect.getBottom() + 4.5f, 0);
cb.endText();
}
}
In this example, the text "PRESS PREVIEW" is added to all the cells to which you've added this event like this:
PdfPCellEvent press = new PressPreview();
PdfPCell cell = new PdfPCell();
cell.setCellEvent(press);
In your case, you'd add two member-variable to the custom event. One PdfTemplate
to which you'll write the value of Y. One int
that keeps track of the number of times the event has been triggered. That's X. When the complete table has been added to the document, you Y will be equal to X and you can fill out this value on the PdfTemplate
.
Summarized: what you want isn't impossible, but it's not trivial.
Upvotes: 1