Cell and table widths

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

31st May 2016
admin-marketing

Switch code for this example

ColumnWidthExample.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.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/26628678/manipulating-witdh-and-height-of-rows-and-cells-in-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.DeviceGray;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.geom.PageSize;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.layout.property.TextAlignment;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32.  
  33. @Category(SampleTest.class)
  34. public class ColumnWidthExample extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/column_width_example.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ColumnWidthExample().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         // Note that it is not necessary to create new PageSize object,
  47.         // but for testing reasons (connected to parallelization) we call constructor here
  48.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  49.  
  50.         float[] columnWidths = {1, 5, 5};
  51.         Table table = new Table(columnWidths);
  52.         table.setWidthPercent(100);
  53.         PdfFont f = PdfFontFactory.createFont(FontConstants.HELVETICA);
  54.         Cell cell = new Cell(1, 3)
  55.                 .add(new Paragraph("This is a header"))
  56.                 .setFont(f)
  57.                 .setFontSize(13)
  58.                 .setFontColor(DeviceGray.WHITE)
  59.                 .setBackgroundColor(DeviceGray.BLACK)
  60.                 .setTextAlignment(TextAlignment.CENTER);
  61.         table.addHeaderCell(cell);
  62.         for (int i = 0; i < 2; i++) {
  63.             Cell[] headerFooter = new Cell[]{
  64.                     new Cell().setBackgroundColor(new DeviceGray(0.75f)).add("#"),
  65.                     new Cell().setBackgroundColor(new DeviceGray(0.75f)).add("Key"),
  66.                     new Cell().setBackgroundColor(new DeviceGray(0.75f)).add("Value")
  67.             };
  68.             for (Cell hfCell : headerFooter) {
  69.                 if (i == 0) {
  70.                     table.addHeaderCell(hfCell);
  71.                 } else {
  72.                     table.addFooterCell(hfCell);
  73.                 }
  74.             }
  75.         }
  76.         for (int counter = 1; counter < 101; counter++) {
  77.             table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add(String.valueOf(counter)));
  78.             table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add("key " + counter));
  79.             table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add("value " + counter));
  80.         }
  81.         doc.add(table);
  82.         doc.close();
  83.     }
  84. }
FullPageTable.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/19873263/how-to-increase-the-width-of-pdfptable-in-itext-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.color.DeviceRgb;
  15. import com.itextpdf.kernel.geom.PageSize;
  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.property.TextAlignment;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class FullPageTable extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/tables/full_page_table.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new FullPageTable().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Document doc = new Document(pdfDoc, new PageSize(595, 842));
  44.         doc.setMargins(0, 0, 0, 0);
  45.  
  46.         Table table = new Table(10);
  47.         table.setMarginTop(0);
  48.         table.setMarginBottom(0);
  49.         // first row
  50.         Cell cell = new Cell(1, 10).add(new Paragraph("DateRange"));
  51.         cell.setTextAlignment(TextAlignment.CENTER);
  52.         cell.setPadding(5);
  53.         cell.setBackgroundColor(new DeviceRgb(140, 221, 8));
  54.         table.addCell(cell);
  55.  
  56.         table.addCell("Calldate");
  57.         table.addCell("Calltime");
  58.         table.addCell("Source");
  59.         table.addCell("DialedNo");
  60.         table.addCell("Extension");
  61.         table.addCell("Trunk");
  62.         table.addCell("Duration");
  63.         table.addCell("Calltype");
  64.         table.addCell("Callcost");
  65.         table.addCell("Site");
  66.  
  67.         for (int i = 0; i < 100; i++) {
  68.             table.addCell("date" + i);
  69.             table.addCell("time" + i);
  70.             table.addCell("source" + i);
  71.             table.addCell("destination" + i);
  72.             table.addCell("extension" + i);
  73.             table.addCell("trunk" + i);
  74.             table.addCell("dur" + i);
  75.             table.addCell("toc" + i);
  76.             table.addCell("callcost" + i);
  77.             table.addCell("Site" + i);
  78.         }
  79.         doc.add(table);
  80.  
  81.         doc.close();
  82.     }
  83. }
RightCornerTable.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/33440294/create-table-in-itext-pdf-in-java
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.PageSize;
  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.property.HorizontalAlignment;
  20. import com.itextpdf.layout.border.SolidBorder;
  21. import com.itextpdf.layout.element.AreaBreak;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import java.io.File;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class RightCornerTable extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/tables/right_corner_table.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new RightCornerTable().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc, new PageSize(300, 300));
  46.         doc.setMargins(0, 0, 0, 0);
  47.  
  48.         Table table = new Table(1);
  49.         table.setHorizontalAlignment(HorizontalAlignment.RIGHT);
  50.         table.setWidth(90);
  51.         Cell cell = new Cell().add(new Paragraph(" Date").setFontColor(Color.WHITE));
  52.         cell.setBackgroundColor(Color.BLACK);
  53.         cell.setBorder(new SolidBorder(Color.GRAY, 2));
  54.         table.addCell(cell);
  55.         Cell cellTwo = new Cell().add(("10/01/2015"));
  56.         cellTwo.setBorder(new SolidBorder(2));
  57.         table.addCell(cellTwo);
  58.         doc.add(table);
  59.         doc.add(new AreaBreak());
  60.         doc.add(table);
  61.  
  62.         doc.close();
  63.     }
  64. }
SimpleTable3.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/27884229/itextsharap-error-while-adding-pdf-table-to-document
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Cell;
  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. @Category(SampleTest.class)
  28. public class SimpleTable3 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table3.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SimpleTable3().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         // Note that it is not necessary to create new PageSize object,
  41.         // but for testing reasons (connected to parallelization) we call constructor here
  42.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A3).rotate());
  43.  
  44.         Table table = new Table(35);
  45.         table.setWidth(pdfDoc.getDefaultPageSize().getWidth() - 80);
  46.         Cell contractor = new Cell(1, 5).add("XXXXXXXXXXXXX");
  47.         table.addCell(contractor);
  48.         Cell workType = new Cell(1, 5).add("Refractory Works");
  49.         table.addCell(workType);
  50.         Cell supervisor = new Cell(1, 4).add("XXXXXXXXXXXXXX");
  51.         table.addCell(supervisor);
  52.         Cell paySlipHead = new Cell(1, 10).add("XXXXXXXXXXXXXXXX");
  53.         table.addCell(paySlipHead);
  54.         Cell paySlipMonth = new Cell(1, 2).add("XXXXXXX");
  55.         table.addCell(paySlipMonth);
  56.         Cell blank = new Cell(1, 9).add("");
  57.         table.addCell(blank);
  58.         doc.add(table);
  59.  
  60.         doc.close();
  61.     }
  62. }
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