Repeating rows

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

HeaderRowRepeated.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/32582927/itext-pdfwriter-writing-table-header-if-the-few-table-rows-go-to-new-page
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Table;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import java.io.File;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. @Category(SampleTest.class)
  26. public class HeaderRowRepeated extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/tables/header_row_repeated.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new HeaderRowRepeated().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  38.         Document doc = new Document(pdfDoc);
  39.         // table with 2 columns:
  40.         Table table = new Table(2);
  41.         // header row:
  42.         table.addHeaderCell("Key");
  43.         table.addHeaderCell("Value");
  44.         table.setSkipFirstHeader(true);
  45.         // many data rows:
  46.         for (int i = 1; i < 51; i++) {
  47.             table.addCell("key: " + i);
  48.             table.addCell("value: " + i);
  49.         }
  50.         doc.add(table);
  51.         doc.close();
  52.     }
  53. }
RepeatLastRows.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.layout.renderer.TableRenderer;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class RepeatLastRows extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/repeat_last_rows.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new RepeatLastRows().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         Table table = new Table(1);
  44.         table.setWidth(523);
  45.         table.setNextRenderer(new RepeatTableRenderer(table, new Table.RowRange(0, 113)));
  46.         // the number is changed in order to provide the same as in itext5 example
  47.         for (int i = 1; i < 115; i++)
  48.             table.addCell(new Cell().add(new Paragraph("row " + i)));
  49.         doc.add(table);
  50.  
  51.         doc.close();
  52.     }
  53.  
  54.  
  55.     private class RepeatTableRenderer extends TableRenderer {
  56.         public RepeatTableRenderer(Table modelElement, Table.RowRange rowRange) {
  57.             super(modelElement, rowRange);
  58.         }
  59.  
  60.         protected RepeatTableRenderer(Table modelElement) {
  61.             super(modelElement);
  62.         }
  63.  
  64.         @Override
  65.         public RepeatTableRenderer getNextRenderer() {
  66.             return new RepeatTableRenderer((Table) modelElement);
  67.         }
  68.  
  69.         @Override
  70.         protected TableRenderer[] split(int row) {
  71.             RepeatTableRenderer splitRenderer = (RepeatTableRenderer) createSplitRenderer(
  72.                     new Table.RowRange(rowRange.getStartRow(), rowRange.getStartRow() + row));
  73.             splitRenderer.rows = rows.subList(0, row);
  74.             RepeatTableRenderer overflowRenderer;
  75.             if (rows.size() - row > 5) {
  76.                 overflowRenderer = (RepeatTableRenderer) createOverflowRenderer(
  77.                         new Table.RowRange(rowRange.getStartRow() + row, rowRange.getFinishRow()));
  78.                 overflowRenderer.rows = rows.subList(row, rows.size());
  79.             } else {
  80.                 overflowRenderer = (RepeatTableRenderer) createOverflowRenderer(
  81.                         new Table.RowRange(rowRange.getFinishRow() - 5, rowRange.getFinishRow()));
  82.                 overflowRenderer.rows = rows.subList(rows.size() - 5, rows.size());
  83.             }
  84.             splitRenderer.occupiedArea = occupiedArea;
  85.             return new TableRenderer[]{splitRenderer, overflowRenderer};
  86.         }
  87.     }
  88. }
RepeatLastRows2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.layout.renderer.TableRenderer;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class RepeatLastRows2 extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/repeat_last_rows2.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new RepeatLastRows2().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         Table table = new Table(1);
  44.         table.setWidth(523);
  45.         table.setNextRenderer(new RepeatTableRenderer(table, new Table.RowRange(0, 100)));
  46.         for (int i = 1; i < 100; i++)
  47.             table.addCell(new Cell().add(new Paragraph("row " + i)));
  48.         doc.add(table);
  49.  
  50.         doc.close();
  51.     }
  52.  
  53.  
  54.     private class RepeatTableRenderer extends TableRenderer {
  55.         public RepeatTableRenderer(Table modelElement, Table.RowRange rowRange) {
  56.             super(modelElement, rowRange);
  57.         }
  58.  
  59.         protected RepeatTableRenderer(Table modelElement) {
  60.             super(modelElement);
  61.         }
  62.  
  63.         @Override
  64.         public RepeatTableRenderer getNextRenderer() {
  65.             return new RepeatTableRenderer((Table) modelElement);
  66.         }
  67.  
  68.         @Override
  69.         protected TableRenderer[] split(int row) {
  70.             RepeatTableRenderer splitRenderer = (RepeatTableRenderer) createSplitRenderer(
  71.                     new Table.RowRange(rowRange.getStartRow(), rowRange.getStartRow() + row));
  72.             splitRenderer.rows = rows.subList(0, row);
  73.             RepeatTableRenderer overflowRenderer;
  74.             if (row > 5) {
  75.                 overflowRenderer = (RepeatTableRenderer) createOverflowRenderer(
  76.                         new Table.RowRange(rowRange.getStartRow() - 5 + row, rowRange.getFinishRow()));
  77.                 overflowRenderer.rows = rows.subList(row - 5, rows.size());
  78.             } else {
  79.                 overflowRenderer = (RepeatTableRenderer) createOverflowRenderer(
  80.                         new Table.RowRange(rowRange.getStartRow() + row, rowRange.getFinishRow()));
  81.                 overflowRenderer.rows = rows.subList(row, rows.size());
  82.             }
  83.             splitRenderer.occupiedArea = occupiedArea;
  84.             return new TableRenderer[]{splitRenderer, overflowRenderer};
  85.         }
  86.     }
  87. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now