Tables

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

1st September 2016
iText PDF

Switch code for this example

HelloWorldTable.java

  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 5 version of one of the examples.
  6.  */
  7. package javaone.edition16;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class HelloWorldTable {
  25.     public static final String DEST = "results/javaone/edition16/hello_table.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException,
  28.             DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new HelloWorldTable().createPdf(DEST);
  32.     }
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         PdfPTable table = new PdfPTable(3);
  38.         PdfPCell cell = new PdfPCell(new Phrase("Cell with colspan 3"));
  39.         cell.setColspan(3);
  40.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  41.         table.addCell(cell);
  42.         cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  43.         cell.setRowspan(2);
  44.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  45.         table.addCell(cell);
  46.         table.addCell("Cell 1.1");
  47.         cell = new PdfPCell();
  48.         cell.addElement(new Phrase("Cell 1.2"));
  49.         table.addCell(cell);
  50.         cell = new PdfPCell(new Phrase("Cell 2.1"));
  51.         cell.setPadding(5);
  52.         cell.setUseAscender(true);
  53.         cell.setUseDescender(true);
  54.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  55.         table.addCell(cell);
  56.         cell = new PdfPCell();
  57.         cell.setPadding(5);
  58.         cell.setUseAscender(true);
  59.         cell.setUseDescender(true);
  60.         Paragraph p = new Paragraph("Cell 2.2");
  61.         p.setAlignment(Element.ALIGN_CENTER);
  62.         cell.addElement(p);
  63.         table.addCell(cell);
  64.         document.add(table);
  65.         document.close();
  66.     }
  67.  
  68. }

HelloWorldTable.java

  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 7 version of one of the examples.
  6.  */
  7. package com.itextpdf.sandbox.javaone16;
  8.  
  9. import com.itextpdf.kernel.color.Color;
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Cell;
  14. import com.itextpdf.layout.element.Table;
  15. import com.itextpdf.layout.property.TextAlignment;
  16. import com.itextpdf.layout.property.VerticalAlignment;
  17. import com.itextpdf.test.annotations.WrapToTest;
  18. import java.io.File;
  19. import java.io.IOException;
  20.  
  21. @WrapToTest
  22. public class HelloWorldTable {
  23.     public static final String DEST
  24.         = "results/javaone16/hello_table.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new HelloWorldTable().createPdf(DEST);
  30.     }
  31.  
  32.     public void createPdf(String dest) throws IOException {
  33.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  34.         try (Document document = new Document(pdf)) {
  35.             Table table = new Table(3);
  36.             Cell cell = new Cell(1, 3)
  37.                 .setTextAlignment(TextAlignment.CENTER)
  38.                 .add("Cell with colspan 3");
  39.             table.addCell(cell);
  40.             cell = new Cell(2, 1)
  41.                 .add("Cell with rowspan 2")
  42.                 .setVerticalAlignment(VerticalAlignment.MIDDLE);
  43.             table.addCell(cell);
  44.             table.addCell("Cell 1.1");
  45.             table.addCell(new Cell().add("Cell 1.2"));
  46.             table.addCell(new Cell()
  47.                 .add("Cell 2.1")
  48.                 .setBackgroundColor(Color.LIGHT_GRAY)
  49.                 .setMargin(5));
  50.             table.addCell(new Cell()
  51.                 .add("Cell 1.2")
  52.                 .setBackgroundColor(Color.LIGHT_GRAY)
  53.                 .setPadding(5));
  54.             document.add(table);
  55.         }
  56.     }
  57. }
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