Cell heights

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

31st May 2016
admin-marketing

Switch code for this example

CellHeights.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. package com.itextpdf.samples.sandbox.tables;
  9.  
  10. import com.itextpdf.kernel.geom.PageSize;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.Cell;
  15. import com.itextpdf.layout.element.Paragraph;
  16. import com.itextpdf.layout.element.Table;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24.  
  25. @Category(SampleTest.class)
  26. public class CellHeights extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/tables/cell_heights.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new CellHeights().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  38.         // Note that it is not necessary to create new PageSize object,
  39.         // but for testing reasons (connected to parallelization) we call constructor here
  40.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A5).rotate());
  41.  
  42.         Table table = new Table(2);
  43.         // a long phrase with newlines
  44.         Paragraph p = new Paragraph("Dr. iText or:\nHow I Learned to Stop Worrying\nand Love PDF.");
  45.         Cell cell = new Cell().add(p);
  46.         // the phrase fits the fixed height
  47.         table.addCell("fixed height (more than sufficient)");
  48.         cell.setHeight(72f);
  49.         table.addCell(cell.clone(true));
  50.         // the phrase doesn't fit the fixed height
  51.         table.addCell("fixed height (not sufficient)");
  52.         // TODO DEVSIX-555
  53.         cell.setHeight(36f);
  54.         table.addCell(cell.clone(true));
  55.         // The minimum height is exceeded
  56.         table.addCell("minimum height");
  57.         cell = new Cell().add("Dr. iText");
  58.         cell.setHeight(36f);
  59.         table.addCell(cell);
  60.  
  61.         doc.add(table);
  62.         doc.close();
  63.     }
  64. }
FitTableOnPage.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.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.layout.LayoutArea;
  23. import com.itextpdf.layout.layout.LayoutContext;
  24. import com.itextpdf.layout.layout.LayoutResult;
  25. import com.itextpdf.layout.renderer.IRenderer;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import java.io.File;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. @Category(SampleTest.class)
  34. public class FitTableOnPage extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/fit_table_on_page.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new FitTableOnPage().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         Table table = new Table(1);
  46.         for (int i = 0; i < 10; i++) {
  47.             Cell cell;
  48.             if (i == 9) {
  49.                 cell = new Cell().add(new Paragraph("Two\nLines"));
  50.             } else {
  51.                 cell = new Cell().add(new Paragraph(Integer.toString(i)));
  52.             }
  53.             table.addCell(cell);
  54.         }
  55.  
  56.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  57.         Document doc = new Document(pdfDoc);
  58.         IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
  59.         LayoutResult tableLayoutResult =
  60.                 tableRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(550 + 72, 1000))));
  61.  
  62.         pdfDoc.setDefaultPageSize(new PageSize(550 + 72, tableLayoutResult.getOccupiedArea().getBBox().getHeight() + 72));
  63.         doc.add(table);
  64.         doc.close();
  65.     }
  66. }
FixedHeightCell.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.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22093745/itext-how-do-setminimumsize-and-setfixedsize-interact
  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.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27.  
  28. @Category(SampleTest.class)
  29. public class FixedHeightCell extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/fixed_height_cell.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new FixedHeightCell().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(5);
  44.         Cell cell;
  45.         for (int r = 'A'; r
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