1

I am trying to generate a pdf using Itext.where i need to build a table with header. But i am adding all the content on the page dyamically by iterating the list and adding the cells to the Table ,and finally adding table to the Document after the interation is done. document.add(table);

My writer and document doesnt show me a updated page number if my content is moved on to the next page,until the table content is added to the document.I need to iterate and build the dynamic table where at the begining of the next page it should have the header of the table.I tried my level best following many approaches.But,I am unable to know when the content is moved on to next page in the middle of the iteration process.PDFWriter is showing me the page number before entering the loop.I need to get the updated pagenumber when ever the tables content goes on to next page.And i dont need this header to be on all pages.I need it only until the table content carries over.Please let me know which is the best way to handle this situation. Thanks

David
  • 2,987
  • 1
  • 29
  • 35
  • If all you are looking to do is ensure that the header is output on each page where the table rolls over multiple pages then I think all you need to do is call setHeaderRows() on your table instance and iText will take care of the rest.http://stackoverflow.com/questions/11861040/repeat-pdfptable-header-in-all-the-continuation-pages-using-itext – Alan Hay Feb 19 '15 at 15:51
  • @AlanHay I don't think your link solves the problem. If I understand the question correctly (it should be voted to be closed because it is very unclear), the person wants a repeating header that shows a page number in a cell. However: I'm not sure. I won't answer until the question has been updated in a way that people can actually understand what is asked. – Bruno Lowagie Feb 19 '15 at 17:33
  • I am sorry if my question was unclear,I dont want a repeating header and not page number in the cell,I am trying to create a table where there will be dynamic rows,I am able to succesfully do it.But my issue was when the content on the first table goes on to the next one,I need to have a header describing the table content on the next page as well.Which i am trying to do it using the page number.If i know that the content is been sent on to the next page i can intercept and add my header before the content of the table goes to the next page.I hope i am clear this time.hope i am clear this time – user2488076 Feb 19 '15 at 18:12
  • *I hope i am clear this time* - you should edit clarifications into the question itself, too. That been said, you seem to be looking for a dynamic table header line containing something like "table XXZ, continued"? – mkl Feb 19 '15 at 20:27
  • @mkl Yes ,that right a dynamic table.Thanks – user2488076 Feb 19 '15 at 21:06
  • I got my answer table.setHeaderRows(1) .It repeats the header to be on next pages as well until the table iterates.Thank you every one – user2488076 Feb 19 '15 at 23:11
  • @AlanHay I don't think your answer qualifies. If you think your answer is correct, you should have marked this question as duplicate. I don't think it is an exact duplicate. If the OP uses your answer to add *Table XYZ (continued)* as a header, it will also be present on the first page (which probably isn't his intention). Your answer misses a reference to `setSkipFirstHeader()`. – Bruno Lowagie Feb 20 '15 at 10:09

1 Answers1

1

The other answer to this question isn't a real answer, it refers to an answer to another question. If that is actually the answer to this current question, the current question should have been marked as duplicate.

This being said, I think the answer is not entirely correct. I have written an example that produces something like this:

enter image description here

As you can see, there is no header on the first page, there is only a header on the second and third page that says "Table XYZ (Continued)". I even added a footer that says "Continue on next page". As you can see, this footer only appears at the moment the table is split. It isn't present on the last page. This wasn't asked in the question, and it is easy to remove from my code.

You can find the complete code sample here: SimpleTable5

These are the relevant parts:

PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Table XYZ (Continued)"));
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Continue on next page"));
cell.setColspan(5);
table.addCell(cell);
table.setHeaderRows(2);
table.setFooterRows(1);
table.setSkipFirstHeader(true);
table.setSkipLastFooter(true);
for (int i = 0; i < 350; i++) {
    table.addCell(String.valueOf(i+1));
}
document.add(table);

If you only want a header:

PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Table XYZ (Continued)"));
cell.setColspan(5);
table.addCell(cell);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
for (int i = 0; i < 350; i++) {
    table.addCell(String.valueOf(i+1));
}
document.add(table);

The difference with your code, is that you add the following line to avoid that the header appears on the first page:

table.setSkipFirstHeader(true);

This is not mentioned in the answer to Repeat PdfPTable header in all the continuation pages using iText

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Great answer @Bruno Lowagie , But how could I add page number in this dynamic Footer Row table. – Kamal kannan May 14 '17 at 10:06
  • Using a cell event – Bruno Lowagie May 14 '17 at 10:49
  • I used writer.getPageNumber() but it returns only 1 for all my pages – Kamal kannan May 14 '17 at 11:36
  • cell = new PdfPCell(new Phrase("Page No ${writer.getPageNumber()}", new Font(Font.FontFamily.HELVETICA, 8, Font.BOLD) )) cell.setBorder(Rectangle.TOP) cell.setHorizontalAlignment(Element.ALIGN_RIGHT) cell.setVerticalAlignment(Element.ALIGN_MIDDLE) cell.setColspan(header.size()) table.addCell(cell) table.setHeaderRows(headerRows) table.setFooterRows(1) table.setSkipLastFooter(true) – Kamal kannan May 14 '17 at 11:43
  • As you may have noticed, comments are not well-suited to paste code. As a matter of fact, comments aren't well-suited to post questions in general. Questions should be posted using the "Post a question" functionality. This being said. Why would `${writer.getPageNumber()}` work? That's not iText syntax! That's not a page event. Use page events! – Bruno Lowagie May 14 '17 at 12:00