Repeating rows

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

8th November 2015
admin-marketing

Switch code for this example

HeaderRowRepeated.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/32582927/itext-pdfwriter-writing-table-header-if-the-few-table-rows-go-to-new-page
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import sandbox.WrapToTest;
  15.  
  16. /**
  17.  * @author iText
  18.  */
  19. @WrapToTest
  20. public class HeaderRowRepeated {
  21.     public static final String DEST = "results/tables/repeat_header_row.pdf";
  22.    
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new HeaderRowRepeated().createPdf(DEST);
  28.     }    
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         // table with 2 columns:
  34.         PdfPTable table = new PdfPTable(2);
  35.         // header row:
  36.         table.addCell("Key");
  37.         table.addCell("Value");
  38.         table.setHeaderRows(1);
  39.         table.setSkipFirstHeader(true);
  40.         // many data rows:
  41.         for (int i = 1; i < 51; i++) {
  42.             table.addCell("key: " + i);
  43.             table.addCell("value: " + i);
  44.         }
  45.         document.add(table);
  46.         document.close();
  47.     }
  48. }
RepeatLastRows.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class RepeatLastRows {
  21.     public static final String DEST = "results/tables/repeat_last_rows.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new RepeatLastRows().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         // we create a table that spans the width of the page and that has 99 rows
  35.         PdfPTable table = new PdfPTable(1);
  36.         table.setTotalWidth(523);
  37.         for (int i = 1; i < 100; i++)
  38.             table.addCell("row " + i);
  39.         // we add the table at an absolute position (starting at the top of the page)
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.         int currentRowStart = 0;
  42.         int currentRow = 0;
  43.         int totalRows = table.getRows().size();
  44.         while (true) {
  45.             // available height of the page
  46.             float available_height = 770;
  47.             // how many rows fit the height?
  48.             while (available_height > 0 && currentRow < totalRows) {
  49.                 available_height -= table.getRowHeight(currentRow++);
  50.             }
  51.             // we stop as soon as all the rows are counted
  52.             if (currentRow == totalRows)
  53.                 break;
  54.             // we draw part the rows that fit the page and start a new page
  55.             table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
  56.             document.newPage();
  57.             currentRowStart = currentRow;
  58.         }
  59.         // if there are less than 5 rows left, we adjust the row start value
  60.         if (currentRow - currentRowStart < 5)
  61.             currentRowStart = currentRow - 5;
  62.         // we write the remaining rows
  63.         table.writeSelectedRows(currentRowStart, currentRow, 36, 806, canvas);
  64.         document.close();
  65.     }
  66.  
  67. }
RepeatLastRows2.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22153449/print-last-5-rows-to-next-page-itext-java
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfContentByte;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class RepeatLastRows2 {
  21.     public static final String DEST = "results/tables/repeat_last_rows2.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new RepeatLastRows2().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         // we create a table that spans the width of the page and that has 99 rows
  35.         PdfPTable table = new PdfPTable(1);
  36.         table.setTotalWidth(523);
  37.         for (int i = 1; i < 100; i++)
  38.             table.addCell("row " + i);
  39.         // we add the table at an absolute position (starting at the top of the page)
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.         int currentRowStart = 0;
  42.         int currentRow = 0;
  43.         int totalRows = table.getRows().size();
  44.         while (true) {
  45.             // available height of the page
  46.             float available_height = 770;
  47.             // how many rows fit the height?
  48.             while (available_height > 0 && currentRow < totalRows) {
  49.                 available_height -= table.getRowHeight(currentRow++);
  50.             }
  51.             // we stop as soon as all the rows are counted
  52.             if (currentRow == totalRows) {
  53.                 break;
  54.             }
  55.             // we draw part the rows that fit the page and start a new page
  56.             table.writeSelectedRows(currentRowStart, --currentRow, 36, 806, canvas);
  57.             document.newPage();
  58.             currentRowStart = currentRow - 5;
  59.             currentRow -= 5;
  60.             if (currentRow < 1) {
  61.                 currentRow = 1;
  62.                 currentRowStart = 1;
  63.             }
  64.         }
  65.         // we draw the remaining rows
  66.         table.writeSelectedRows(currentRowStart, -1, 36, 806, canvas);
  67.         document.close();
  68.     }
  69.  
  70. }
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