Cell and table widths

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

8th November 2015
admin-marketing

Switch code for this example

ColumnWidthExample.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26628678/manipulating-witdh-and-height-of-rows-and-cells-in-itextsharp
  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.PageSize;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.GrayColor;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class ColumnWidthExample {
  26.    
  27.     public static final String DEST = "results/tables/column_width_example.pdf";
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ColumnWidthExample().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document(PageSize.A4.rotate());
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         float[] columnWidths = {1, 5, 5};
  39.         PdfPTable table = new PdfPTable(columnWidths);
  40.         table.setWidthPercentage(100);
  41.         table.getDefaultCell().setUseAscender(true);
  42.         table.getDefaultCell().setUseDescender(true);
  43.         Font f = new Font(FontFamily.HELVETICA, 13, Font.NORMAL, GrayColor.GRAYWHITE);
  44.         PdfPCell cell = new PdfPCell(new Phrase("This is a header", f));
  45.         cell.setBackgroundColor(GrayColor.GRAYBLACK);
  46.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  47.         cell.setColspan(3);
  48.         table.addCell(cell);
  49.         table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f));
  50.         for (int i = 0; i < 2; i++) {
  51.             table.addCell("#");
  52.             table.addCell("Key");
  53.             table.addCell("Value");
  54.         }
  55.         table.setHeaderRows(3);
  56.         table.setFooterRows(1);
  57.         table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE);
  58.         table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
  59.         for (int counter = 1; counter < 101; counter++) {
  60.             table.addCell(String.valueOf(counter));
  61.             table.addCell("key " + counter);
  62.             table.addCell("value " + counter);
  63.         }
  64.         document.add(table);
  65.         document.close();
  66.     }
  67. }
FullPageTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19873263/how-to-increase-the-width-of-pdfptable-in-itext-pdf
  4.  *
  5.  * We create a table with two columns and two cells.
  6.  * This way, we can add two images next to each other.
  7.  */
  8. package sandbox.tables;
  9.  
  10. import com.itextpdf.text.BaseColor;
  11. import com.itextpdf.text.Document;
  12. import com.itextpdf.text.DocumentException;
  13. import com.itextpdf.text.Element;
  14. import com.itextpdf.text.PageSize;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class FullPageTable {
  28.  
  29.     public static final String DEST = "results/tables/full_page_table.pdf";
  30.  
  31.     public static void main(String[] args) throws IOException,
  32.             DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new FullPageTable().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Document document = new Document(PageSize.A4, 0, 0, 0, 0);
  40.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.         document.open();
  42.         PdfPTable table = new PdfPTable(10);
  43.  
  44.         table.setWidthPercentage(100);
  45.         table.setSpacingBefore(0f);
  46.         table.setSpacingAfter(0f);
  47.  
  48.         // first row
  49.         PdfPCell cell = new PdfPCell(new Phrase("DateRange"));
  50.         cell.setColspan(10);
  51.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  52.         cell.setPadding(5.0f);
  53.         cell.setBackgroundColor(new BaseColor(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.         document.add(table);
  80.         document.close();
  81.     }
  82. }
RightCornerTable.java
  1. /**
  2.  * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
  3.  * http://stackoverflow.com/questions/33440294/create-table-in-itext-pdf-in-java
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Element;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class RightCornerTable {
  26.     public static final String DEST = "results/tables/right_corner_table.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException,
  29.             DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new RightCornerTable().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Rectangle pagesize = new Rectangle(300, 300);
  37.         Document document = new Document(pagesize, 0, 0, 0, 0);
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         document.open();
  40.         PdfPTable table = new PdfPTable(1);
  41.         table.setHorizontalAlignment(Element.ALIGN_RIGHT);
  42.         table.setWidthPercentage(30);
  43.         Font white = new Font();
  44.         white.setColor(BaseColor.WHITE);
  45.         PdfPCell cell = new PdfPCell(new Phrase(" Date" , white));
  46.         cell.setBackgroundColor(BaseColor.BLACK);
  47.         cell.setBorderColor(BaseColor.GRAY);
  48.         cell.setBorderWidth(2f);
  49.         table.addCell(cell);
  50.         PdfPCell cellTwo = new PdfPCell(new Phrase("10/01/2015"));
  51.         cellTwo.setBorderWidth(2f);
  52.         table.addCell(cellTwo);
  53.         document.add(table);
  54.         document.newPage();
  55.         table.setTotalWidth(90);
  56.         PdfContentByte canvas = writer.getDirectContent();
  57.         table.writeSelectedRows(0, -1, document.right() - 90, document.top(), canvas);
  58.         document.close();
  59.     }
  60.  
  61. }
SimpleTable3.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27884229/itextsharap-error-while-adding-pdf-table-to-document
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class SimpleTable3 {
  22.     public static final String DEST = "results/tables/simple_table3.pdf";
  23.  
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new SimpleTable3().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document(PageSize.A3.rotate());
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         PdfPTable table = new PdfPTable(35);
  35.         table.setTotalWidth(document.getPageSize().getWidth() - 80);
  36.         table.setLockedWidth(true);
  37.         PdfPCell contractor = new PdfPCell(new Phrase("XXXXXXXXXXXXX"));
  38.         contractor.setColspan(5);
  39.         table.addCell(contractor);
  40.         PdfPCell workType = new PdfPCell(new Phrase("Refractory Works"));
  41.         workType.setColspan(5);
  42.         table.addCell(workType);
  43.         PdfPCell supervisor = new PdfPCell(new Phrase("XXXXXXXXXXXXXX"));
  44.         supervisor.setColspan(4);
  45.         table.addCell(supervisor);
  46.         PdfPCell paySlipHead = new PdfPCell(new Phrase("XXXXXXXXXXXXXXXX"));
  47.         paySlipHead.setColspan(10);
  48.         table.addCell(paySlipHead);
  49.         PdfPCell paySlipMonth = new PdfPCell(new Phrase("XXXXXXX"));
  50.         paySlipMonth.setColspan(2);
  51.         table.addCell(paySlipMonth);
  52.         PdfPCell blank = new PdfPCell(new Phrase(""));
  53.         blank.setColspan(9);
  54.         table.addCell(blank);
  55.         document.add(table);
  56.         document.close();
  57.     }
  58.  
  59. }
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