Colspan and rowspan

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

8th November 2015
admin-marketing

Switch code for this example

SimpleRowColspan.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/20016630/how-to-create-a-table-in-a-generated-pdf-using-itextsharp
  4.  *
  5.  * We create a table with five columns, combining rowspan and colspan
  6.  */
  7. package sandbox.tables;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class SimpleRowColspan {
  24.     public static final String DEST = "results/tables/simple_rowspan_colspan.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException,
  27.             DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new SimpleRowColspan().createPdf(DEST);
  31.     }
  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(5);
  38.         table.setWidths(new int[]{ 1, 2, 2, 2, 1});
  39.         PdfPCell cell;
  40.         cell = new PdfPCell(new Phrase("S/N"));
  41.         cell.setRowspan(2);
  42.         table.addCell(cell);
  43.         cell = new PdfPCell(new Phrase("Name"));
  44.         cell.setColspan(3);
  45.         table.addCell(cell);
  46.         cell = new PdfPCell(new Phrase("Age"));
  47.         cell.setRowspan(2);
  48.         table.addCell(cell);
  49.         table.addCell("SURNAME");
  50.         table.addCell("FIRST NAME");
  51.         table.addCell("MIDDLE NAME");
  52.         table.addCell("1");
  53.         table.addCell("James");
  54.         table.addCell("Fish");
  55.         table.addCell("Stone");
  56.         table.addCell("17");
  57.         document.add(table);
  58.         document.close();
  59.     }
  60. }
ColspanRowspan.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23989852/itext-combining-rowspan-and-colspan-pdfptable
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class ColspanRowspan {
  21.  
  22.     public static final String DEST = "results/tables/colspan_rowspan.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new ColspanRowspan().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         PdfPTable table = new PdfPTable(4);
  35.         PdfPCell cell = new PdfPCell(new Phrase(" 1,1 "));
  36.         table.addCell(cell);
  37.         cell = new PdfPCell(new Phrase(" 1,2 "));
  38.         table.addCell(cell);
  39.         PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));
  40.         cell23.setColspan(2);
  41.         cell23.setRowspan(2);
  42.         table.addCell(cell23);
  43.         cell = new PdfPCell(new Phrase(" 2,1 "));
  44.         table.addCell(cell);
  45.         cell = new PdfPCell(new Phrase(" 2,2 "));
  46.         table.addCell(cell);
  47.         document.add(table);
  48.         document.close();
  49.     }
  50. }
SimpleTable2.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.Phrase;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class SimpleTable2 {
  21.     public static final String DEST = "results/tables/simple_table2.pdf";
  22.  
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new SimpleTable2().createPdf(DEST);
  28.     }
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         PdfPTable table = new PdfPTable(8);
  34.         PdfPCell cell = new PdfPCell(new Phrase("hi"));
  35.         cell.setRowspan(2);
  36.         table.addCell(cell);
  37.         for(int aw = 0; aw < 14; aw++){
  38.             table.addCell("hi");
  39.         }
  40.         document.add(table);
  41.         document.close();
  42.     }
  43.  
  44. }
SimpleTable9.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30032558/how-to-show-a-cellcolumn-with-its-row-values-of-a-pdftableitextsharp-in-next
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class SimpleTable9 {
  20.     public static final String DEST = "results/tables/simple_table9.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException,
  23.             DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new SimpleTable9().createPdf(DEST);
  27.     }
  28.     public void createPdf(String dest) throws IOException, DocumentException {
  29.         Document document = new Document();
  30.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  31.         document.open();
  32.         document.add(new Paragraph("With 3 columns:"));
  33.         PdfPTable table = new PdfPTable(3);
  34.         table.setSpacingBefore(5);
  35.         table.setWidths(new int[]{1, 1, 8});
  36.         table.setWidthPercentage(100);
  37.         table.addCell("Col a");
  38.         table.addCell("Col b");
  39.         table.addCell("Col c");
  40.         table.addCell("Value a");
  41.         table.addCell("Value b");
  42.         table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
  43.         document.add(table);
  44.         document.add(new Paragraph("With 2 columns:"));
  45.         table = new PdfPTable(2);
  46.         table.setSpacingBefore(5);
  47.         table.setWidthPercentage(100);
  48.         table.getDefaultCell().setColspan(1);
  49.         table.addCell("Col a");
  50.         table.addCell("Col b");
  51.         table.addCell("Value a");
  52.         table.addCell("Value b");
  53.         table.getDefaultCell().setColspan(2);
  54.         table.addCell("Value b");
  55.         table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
  56.         table.getDefaultCell().setColspan(1);
  57.         table.addCell("Col a");
  58.         table.addCell("Col b");
  59.         table.addCell("Value a");
  60.         table.addCell("Value b");
  61.         table.getDefaultCell().setColspan(2);
  62.         table.addCell("Value b");
  63.         table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
  64.         document.add(table);
  65.         document.close();
  66.     }
  67.  
  68. }
SimpleTable11.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31146249/how-to-set-up-a-table-display-in-itextpdf
  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.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 SimpleTable11 {
  22.     public static final String DEST = "results/tables/simple_table11.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 SimpleTable11().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         PdfPTable table = new PdfPTable(5);
  35.         table.setWidths(new int[]{1, 2, 1, 1, 1});
  36.         table.addCell(createCell("SKU", 2, 1, Element.ALIGN_LEFT));
  37.         table.addCell(createCell("Description", 2, 1, Element.ALIGN_LEFT));
  38.         table.addCell(createCell("Unit Price", 2, 1, Element.ALIGN_LEFT));
  39.         table.addCell(createCell("Quantity", 2, 1, Element.ALIGN_LEFT));
  40.         table.addCell(createCell("Extension", 2, 1, Element.ALIGN_LEFT));
  41.         String[][] data = {
  42.             {"ABC123", "The descriptive text may be more than one line and the text should wrap automatically", "$5.00", "10", "$50.00"},
  43.             {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
  44.             {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
  45.         };
  46.         for (String[] row : data) {
  47.             table.addCell(createCell(row[0], 1, 1, Element.ALIGN_LEFT));
  48.             table.addCell(createCell(row[1], 1, 1, Element.ALIGN_LEFT));
  49.             table.addCell(createCell(row[2], 1, 1, Element.ALIGN_RIGHT));
  50.             table.addCell(createCell(row[3], 1, 1, Element.ALIGN_RIGHT));
  51.             table.addCell(createCell(row[4], 1, 1, Element.ALIGN_RIGHT));
  52.         }
  53.         table.addCell(createCell("Totals", 2, 4, Element.ALIGN_LEFT));
  54.         table.addCell(createCell("$1,552.00", 2, 1, Element.ALIGN_RIGHT));
  55.         document.add(table);
  56.         document.close();
  57.     }
  58.    
  59.     public PdfPCell createCell(String content, float borderWidth, int colspan, int alignment) {
  60.         PdfPCell cell = new PdfPCell(new Phrase(content));
  61.         cell.setBorderWidth(borderWidth);
  62.         cell.setColspan(colspan);
  63.         cell.setHorizontalAlignment(alignment);
  64.         return cell;
  65.     }
  66.  
  67. }
SimpleTable12.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31263533/how-to-create-nested-column-using-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.Phrase;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class SimpleTable12 {
  24.     public static final String DEST = "results/tables/simple_table12.pdf";
  25.    
  26.     protected Font font;
  27.  
  28.     public static void main(String[] args) throws IOException,
  29.             DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new SimpleTable12().createPdf(DEST);
  33.     }
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         font = new Font(FontFamily.HELVETICA, 10);
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         PdfPTable table = new PdfPTable(8);
  40.         table.setWidthPercentage(100);
  41.         table.addCell(createCell("Examination", 1, 2, PdfPCell.BOX));
  42.         table.addCell(createCell("Board", 1, 2, PdfPCell.BOX));
  43.         table.addCell(createCell("Month and Year of Passing", 1, 2, PdfPCell.BOX));
  44.         table.addCell(createCell("", 1, 1, PdfPCell.TOP));
  45.         table.addCell(createCell("Marks", 2, 1, PdfPCell.TOP));
  46.         table.addCell(createCell("Percentage", 1, 2, PdfPCell.BOX));
  47.         table.addCell(createCell("Class / Grade", 1, 2, PdfPCell.BOX));
  48.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  49.         table.addCell(createCell("Obtained", 1, 1, PdfPCell.BOX));
  50.         table.addCell(createCell("Out of", 1, 1, PdfPCell.BOX));
  51.         table.addCell(createCell("12th / I.B. Diploma", 1, 1, PdfPCell.BOX));
  52.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  53.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  54.         table.addCell(createCell("Aggregate (all subjects)", 1, 1, PdfPCell.BOX));
  55.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  56.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  57.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  58.         table.addCell(createCell("", 1, 1, PdfPCell.BOX));
  59.         document.add(table);
  60.         document.close();
  61.     }
  62.    
  63.     public PdfPCell createCell(String content, int colspan, int rowspan, int border) {
  64.         PdfPCell cell = new PdfPCell(new Phrase(content, font));
  65.         cell.setColspan(colspan);
  66.         cell.setRowspan(rowspan);
  67.         cell.setBorder(border);
  68.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  69.         return cell;
  70.     }
  71.  
  72. }
RowspanAbsolutePosition.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/23730886/row-span-not-working-for-writeselectedrows-method
  4.  *
  5.  * We create a table with five columns, combining rowspan and colspan
  6.  */
  7. package sandbox.tables;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.Image;
  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 RowspanAbsolutePosition {
  25.     public static final String DEST = "results/tables/write_selected_colspan.pdf";
  26.     public static final String IMG = "resources/images/berlin2013.jpg";
  27.  
  28.     public static void main(String[] args) throws IOException,
  29.             DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new RowspanAbsolutePosition().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         PdfPTable table1 = new PdfPTable(3);
  40.         table1.setWidths(new int[]{15,20,20});
  41.         table1.setTotalWidth(555);
  42.         PdfPCell cell = new PdfPCell(new Phrase("{Month}"));
  43.         cell.setColspan(2);
  44.         cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  45.         Image img = Image.getInstance(IMG);
  46.         img.scaleToFit(555f * 20f / 55f, 10000);
  47.         PdfPCell cell2 = new PdfPCell(img, false);
  48.         cell2.setRowspan(2);
  49.         PdfPCell cell3 = new PdfPCell(new Phrase("Mr Fname Lname"));
  50.         cell3.setColspan(2);
  51.         cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
  52.         table1.addCell(cell);
  53.         table1.addCell(cell2);
  54.         table1.addCell(cell3);
  55.         table1.writeSelectedRows(0, -1, 20 , 820, writer.getDirectContent());
  56.         document.close();
  57.     }
  58. }
TableMeasurements.java
  1. /*
  2.  * Example written in answer to:
  3.  * http://stackoverflow.com/questions/34539028
  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.Paragraph;
  12. import com.itextpdf.text.Utilities;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  *
  22.  * @author Bruno Lowagie (iText Software)
  23.  */
  24. public class TableMeasurements {
  25.     public static final String DEST = "results/tables/table_measurements.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 TableMeasurements().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(10);
  38.         table.setTotalWidth(Utilities.millimetersToPoints(100));
  39.         table.setLockedWidth(true);
  40.         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  41.         table.addCell(getCell(10));
  42.         table.addCell(getCell(5));
  43.         table.addCell(getCell(3));
  44.         table.addCell(getCell(2));
  45.         table.addCell(getCell(3));
  46.         table.addCell(getCell(5));
  47.         table.addCell(getCell(1));
  48.         table.completeRow();
  49.         document.add(table);
  50.         document.close();
  51.     }
  52.    
  53.     private PdfPCell getCell(int cm) {
  54.         PdfPCell cell = new PdfPCell();
  55.         cell.setColspan(cm);
  56.         cell.setUseAscender(true);
  57.         cell.setUseDescender(true);
  58.         Paragraph p = new Paragraph(
  59.                 String.format("%smm", 10 * cm),
  60.                 new Font(Font.FontFamily.HELVETICA, 8));
  61.         p.setAlignment(Element.ALIGN_CENTER);
  62.         cell.addElement(p);
  63.         return cell;
  64.     }
  65. }
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