101 - a very simple table

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

31st May 2016
iText PDF

Switch code for this example

SimpleTable.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 and Nishanthi Grashia in answer to the following question:
  10.  * http://stackoverflow.com/questions/24359321/adding-row-to-a-table-in-pdf-using-itext
  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.Table;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25. @Category(SampleTest.class)
  26. public class SimpleTable extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table.pdf";
  28.  
  29.     public static void main(String[] args) throws Exception {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new SimpleTable().manipulatePdf(DEST);
  33.     }
  34.  
  35.     @Override
  36.     protected void manipulatePdf(String dest) throws Exception {
  37.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  38.         Document doc = new Document(pdfDoc);
  39.  
  40.         Table table = new Table(8);
  41.         for (int i = 0; i < 16; i++) {
  42.             table.addCell("hi");
  43.         }
  44.         doc.add(table);
  45.  
  46.         doc.close();
  47.     }
  48. }
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