Cell heights

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

5th November 2015
admin-marketing

Switch code for this example

CellHeights.java
  1. /*
  2.  * Example written by Bruno Lowagie.
  3.  */
  4.  
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class CellHeights {
  22.     /** The resulting PDF file. */
  23.     public static final String DEST = "results/tables/cell_heights.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new CellHeights().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws DocumentException, IOException {
  33.         // step 1
  34.         Document document = new Document(PageSize.A5.rotate());
  35.         // step 2
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         // step 3
  38.         document.open();
  39.         // step 4
  40.         PdfPTable table = new PdfPTable(2);
  41.         // a long phrase
  42.         Phrase p = new Phrase(
  43.             "Dr. iText or: How I Learned to Stop Worrying and Love PDF.");
  44.         PdfPCell cell = new PdfPCell(p);
  45.         // the prhase is wrapped
  46.         table.addCell("wrap");
  47.         cell.setNoWrap(false);
  48.         table.addCell(cell);
  49.         // the phrase isn't wrapped
  50.         table.addCell("no wrap");
  51.         cell.setNoWrap(true);
  52.         table.addCell(cell);
  53.         // a long phrase with newlines
  54.         p = new Phrase(
  55.             "Dr. iText or:\nHow I Learned to Stop Worrying\nand Love PDF.");
  56.         cell = new PdfPCell(p);
  57.         // the phrase fits the fixed height
  58.         table.addCell("fixed height (more than sufficient)");
  59.         cell.setFixedHeight(72f);
  60.         table.addCell(cell);
  61.         // the phrase doesn't fit the fixed height
  62.         table.addCell("fixed height (not sufficient)");
  63.         cell.setFixedHeight(36f);
  64.         table.addCell(cell);
  65.         // The minimum height is exceeded
  66.         table.addCell("minimum height");
  67.         cell = new PdfPCell(new Phrase("Dr. iText"));
  68.         cell.setMinimumHeight(36f);
  69.         table.addCell(cell);
  70.         // The last row is extended
  71.         table.setExtendLastRow(true);
  72.         table.addCell("extend last row");
  73.         table.addCell(cell);
  74.         document.add(table);
  75.         // step 5
  76.         document.close();
  77.     }
  78. }
FixedHeightCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22093745/itext-how-do-setminimumsize-and-setfixedsize-interact
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class FixedHeightCell {
  22.    
  23.     public static final String DEST = "results/tables/fixed_height_cell.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new FixedHeightCell().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         PdfPTable table = new PdfPTable(5);
  36.         table.setWidthPercentage(100);
  37.         PdfPCell cell;
  38.         for (int r = 'A'; r
FitTableOnPage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
  4.  */
  5.  
  6. package sandbox.tables;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class FitTableOnPage {
  23.     public static final String DEST = "results/tables/fit_table_on_page.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new FitTableOnPage().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         PdfPTable table = new PdfPTable(1);
  33.         table.setTotalWidth(550);
  34.         table.setLockedWidth(true);
  35.         for (int i = 0; i < 10; i++) {
  36.             PdfPCell cell;
  37.             if (i == 9) {
  38.                 cell = new PdfPCell(new Phrase("Two\nLines"));
  39.             }
  40.             else {
  41.                 cell = new PdfPCell(new Phrase(Integer.toString(i)));
  42.             }
  43.             table.addCell(cell);
  44.         }
  45.         Document document = new Document(new Rectangle(612, table.getTotalHeight() + 72));
  46.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  47.         document.open();
  48.         document.add(table);
  49.         document.close();
  50.     }
  51. }
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