101 - a very simple table

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

8th November 2015
admin-marketing

Switch code for this example

SimpleTable.java
  1. /**
  2.  * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
  3.  * http://stackoverflow.com/questions/24359321/adding-row-to-a-table-in-pdf-using-itext
  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.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class SimpleTable {
  19.     public static final String DEST = "results/tables/simple_table.pdf";
  20.  
  21.     public static void main(String[] args) throws IOException,
  22.             DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new SimpleTable().createPdf(DEST);
  26.     }
  27.     public void createPdf(String dest) throws IOException, DocumentException {
  28.         Document document = new Document();
  29.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  30.         document.open();
  31.         PdfPTable table = new PdfPTable(8);
  32.         for(int aw = 0; aw < 16; aw++){
  33.             table.addCell("hi");
  34.         }
  35.         document.add(table);
  36.         document.close();
  37.     }
  38.  
  39. }
SmallTable.java
  1. /*
  2.  * This example was written in answer to the question
  3.  * http://stackoverflow.com/questions/39203479
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.Barcode128;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfPCell;
  18. import com.itextpdf.text.pdf.PdfPTable;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. /**
  26.  * @author Bruno Lowagie (iText Software)
  27.  */
  28. @WrapToTest
  29. public class SmallTable {
  30.     public static final String DEST = "results/tables/small_table.pdf";
  31.  
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new SmallTable().createPdf(DEST);
  36.     }
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Rectangle small = new Rectangle(290,100);
  39.         Font smallfont = new Font(FontFamily.HELVETICA, 10);
  40.         Document document = new Document(small, 5, 5, 5, 5);
  41.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  42.         document.open();
  43.         PdfPTable table = new PdfPTable(2);
  44.         table.setTotalWidth(new float[]{ 160, 120 });
  45.         table.setLockedWidth(true);
  46.         PdfContentByte cb = writer.getDirectContent();
  47.         // first row
  48.         PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
  49.         cell.setFixedHeight(30);
  50.         cell.setBorder(Rectangle.NO_BORDER);
  51.         cell.setColspan(2);
  52.         table.addCell(cell);
  53.         // second row
  54.         cell = new PdfPCell(new Phrase("Some more text", smallfont));
  55.         cell.setFixedHeight(30);
  56.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  57.         cell.setBorder(Rectangle.NO_BORDER);
  58.         table.addCell(cell);
  59.         Barcode128 code128 = new Barcode128();
  60.         code128.setCode("14785236987541");
  61.         code128.setCodeType(Barcode128.CODE128);
  62.         Image code128Image = code128.createImageWithBarcode(cb, null, null);
  63.         cell = new PdfPCell(code128Image, true);
  64.         cell.setBorder(Rectangle.NO_BORDER);
  65.         cell.setFixedHeight(30);
  66.         table.addCell(cell);
  67.         // third row
  68.         table.addCell(cell);
  69.         cell = new PdfPCell(new Phrase("and something else here", smallfont));
  70.         cell.setBorder(Rectangle.NO_BORDER);
  71.         cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
  72.         table.addCell(cell);
  73.         document.add(table);
  74.         document.close();
  75.     }
  76. }
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