Colspan and rowspan

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

31st May 2016
iText PDF

Switch code for this example

ColspanRowspan.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/23989852/itext-combining-rowspan-and-colspan-pdfptable
  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.Cell;
  18. import com.itextpdf.layout.element.Table;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class ColspanRowspan extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/tables/colspan_rowspan.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new ColspanRowspan().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         Document doc = new Document(pdfDoc);
  40.  
  41.         Table table = new Table(4);
  42.         Cell cell = new Cell().add(" 1,1 ");
  43.         table.addCell(cell);
  44.         cell = new Cell().add(" 1,2 ");
  45.         table.addCell(cell);
  46.         Cell cell23 = new Cell(2, 2).add("multi 1,3 and 1,4");
  47.         table.addCell(cell23);
  48.         cell = new Cell().add(" 2,1 ");
  49.         table.addCell(cell);
  50.         cell = new Cell().add(" 2,2 ");
  51.         table.addCell(cell);
  52.         doc.add(table);
  53.  
  54.         doc.close();
  55.     }
  56. }
RowspanAbsolutePosition.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/23730886/row-span-not-working-for-writeselectedrows-method
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Image;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.property.HorizontalAlignment;
  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 RowspanAbsolutePosition extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/tables/rowspan_absolute_position.pdf";
  33.     public static final String IMG = "./src/test/resources/img/berlin2013.jpg";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new RowspanAbsolutePosition().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  44.         Document doc = new Document(pdfDoc);
  45.  
  46.         Table table1 = new Table(new float[]{15, 20, 20});
  47.         table1.setWidth(555);
  48.         Cell cell = new Cell(1, 2).add(new Paragraph("{Month}"));
  49.         cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
  50.         Image img = new Image(ImageDataFactory.create(IMG));
  51.         img.scaleToFit(555f * 20f / 55f, 10000);
  52.         Cell cell2 = new Cell(2, 1).add(img.setAutoScale(true));
  53.         Cell cell3 = new Cell(1, 2).add(new Paragraph("Mr Fname Lname"));
  54.         cell3.setHorizontalAlignment(HorizontalAlignment.LEFT);
  55.         table1.addCell(cell);
  56.         table1.addCell(cell2);
  57.         table1.addCell(cell3);
  58.         doc.add(table1);
  59.  
  60.         doc.close();
  61.     }
  62. }
SimpleRowColspan.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/20016630/how-to-create-a-table-in-a-generated-pdf-using-itextsharp
  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.Cell;
  18. import com.itextpdf.layout.element.Table;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class SimpleRowColspan extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_row_colspan.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new SimpleRowColspan().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         Document doc = new Document(pdfDoc);
  40.  
  41.         Table table = new Table(new float[]{1, 2, 2, 2, 1});
  42.         table.setWidthPercent(100);
  43.  
  44.         Cell cell;
  45.         cell = new Cell(2, 1).add("S/N");
  46.         table.addCell(cell);
  47.         cell = new Cell(1, 3).add("Name");
  48.         table.addCell(cell);
  49.         cell = new Cell(2, 1).add("Age");
  50.         table.addCell(cell);
  51.         table.addCell("SURNAME");
  52.         table.addCell("FIRST NAME");
  53.         table.addCell("MIDDLE NAME");
  54.         table.addCell("1");
  55.         table.addCell("James");
  56.         table.addCell("Fish");
  57.         table.addCell("Stone");
  58.         table.addCell("17");
  59.         doc.add(table);
  60.  
  61.         doc.close();
  62.     }
  63. }
SimpleTable11.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/31146249/how-to-set-up-a-table-display-in-itextpdf
  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.border.SolidBorder;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.property.TextAlignment;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class SimpleTable11 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table11.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new SimpleTable11().manipulatePdf(DEST);
  37.     }
  38.  
  39.     public Cell createCell(String content, float borderWidth, int colspan, TextAlignment alignment) {
  40.         Cell cell = new Cell(1, colspan).add(new Paragraph(content));
  41.         cell.setTextAlignment(alignment);
  42.         cell.setBorder(new SolidBorder(borderWidth));
  43.         return cell;
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.         Document doc = new Document(pdfDoc);
  50.  
  51.         Table table = new Table(new float[]{1, 2, 1, 1, 1});
  52.         table.setWidthPercent(100);
  53.         table.addCell(createCell("SKU", 2, 1, TextAlignment.LEFT));
  54.         table.addCell(createCell("Description", 2, 1, TextAlignment.LEFT));
  55.         table.addCell(createCell("Unit Price", 2, 1, TextAlignment.LEFT));
  56.         table.addCell(createCell("Quantity", 2, 1, TextAlignment.LEFT));
  57.         table.addCell(createCell("Extension", 2, 1, TextAlignment.LEFT));
  58.         String[][] data = {
  59.                 {"ABC123", "The descriptive text may be more than one line and the text should wrap automatically",
  60.                         "$5.00", "10", "$50.00"},
  61.                 {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
  62.                 {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
  63.         };
  64.         for (String[] row : data) {
  65.             table.addCell(createCell(row[0], 1, 1, TextAlignment.LEFT));
  66.             table.addCell(createCell(row[1], 1, 1, TextAlignment.LEFT));
  67.             table.addCell(createCell(row[2], 1, 1, TextAlignment.RIGHT));
  68.             table.addCell(createCell(row[3], 1, 1, TextAlignment.RIGHT));
  69.             table.addCell(createCell(row[4], 1, 1, TextAlignment.RIGHT));
  70.         }
  71.         table.addCell(createCell("Totals", 2, 4, TextAlignment.LEFT));
  72.         table.addCell(createCell("$1,552.00", 2, 1, TextAlignment.RIGHT));
  73.         doc.add(table);
  74.  
  75.         doc.close();
  76.     }
  77. }
SimpleTable12.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/31263533/how-to-create-nested-column-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.border.SolidBorder;
  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 SimpleTable12 extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table12.pdf";
  36.  
  37.     protected PdfFont font;
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new SimpleTable12().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public Cell createCell(String content, int colspan, int rowspan, int border) {
  46.         Cell cell = new Cell(rowspan, colspan).add(new Paragraph(content).setFont(font).setFontSize(10));
  47.         cell.setBorder(null);
  48.         if (8 == (border & 8)) {
  49.             cell.setBorderRight(new SolidBorder(1));
  50.             cell.setBorderBottom(new SolidBorder(1));
  51.         }
  52.         if (4 == (border & 4)) {
  53.             cell.setBorderLeft(new SolidBorder(1));
  54.         }
  55.         if (2 == (border & 2)) {
  56.             cell.setBorderBottom(new SolidBorder(1));
  57.         }
  58.         if (1 == (border & 1)) {
  59.             cell.setBorderTop(new SolidBorder(1));
  60.         }
  61.         cell.setTextAlignment(TextAlignment.CENTER);
  62.         return cell;
  63.     }
  64.  
  65.     @Override
  66.     protected void manipulatePdf(String dest) throws Exception {
  67.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  68.         // Note that it is not necessary to create new PageSize object,
  69.         // but for testing reasons (connected to parallelization) we call constructor here
  70.         Document doc = new Document(pdfDoc, PageSize.A4.rotate());
  71.  
  72.         font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  73.         Table table = new Table(8);
  74.         table.addCell(createCell("Examination", 1, 2, 15));
  75.         table.addCell(createCell("Board", 1, 2, 15));
  76.         table.addCell(createCell("Month and Year of Passing", 1, 2, 15));
  77.         table.addCell(createCell("", 1, 1, 1));
  78.         table.addCell(createCell("Marks", 2, 1, 1));
  79.         table.addCell(createCell("Percentage", 1, 2, 15));
  80.         table.addCell(createCell("Class / Grade", 1, 2, 15));
  81.         table.addCell(createCell("", 1, 1, 15));
  82.         table.addCell(createCell("Obtained", 1, 1, 15));
  83.         table.addCell(createCell("Out of", 1, 1, 15));
  84.         table.addCell(createCell("12th / I.B. Diploma", 1, 1, 15));
  85.         table.addCell(createCell("", 1, 1, 15));
  86.         table.addCell(createCell("", 1, 1, 15));
  87.         table.addCell(createCell("Aggregate (all subjects)", 1, 1, 15));
  88.         table.addCell(createCell("", 1, 1, 15));
  89.         table.addCell(createCell("", 1, 1, 15));
  90.         table.addCell(createCell("", 1, 1, 15));
  91.         table.addCell(createCell("", 1, 1, 15));
  92.         doc.add(table);
  93.  
  94.         doc.close();
  95.     }
  96. }
SimpleTable2.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.Cell;
  18. import com.itextpdf.layout.element.Table;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class SimpleTable2 extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table2.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new SimpleTable2().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  39.         Document doc = new Document(pdfDoc);
  40.  
  41.         Table table = new Table(8);
  42.         Cell cell = new Cell(2, 1).add("hi");
  43.         table.addCell(cell);
  44.         for (int i = 0; i < 14; i++) {
  45.             table.addCell("hi");
  46.         }
  47.         doc.add(table);
  48.  
  49.         doc.close();
  50.     }
  51. }
SimpleTable9.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/30032558/how-to-show-a-cellcolumn-with-its-row-values-of-a-pdftableitextsharp-in-next
  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.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  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 SimpleTable9 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table9.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SimpleTable9().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         Document doc = new Document(pdfDoc);
  41.  
  42.         doc.add(new Paragraph("With 3 columns:"));
  43.         Table table = new Table(new float[]{1, 1, 8});
  44.         table.setWidthPercent(100);
  45.         table.setMarginTop(5);
  46.         table.addCell("Col a");
  47.         table.addCell("Col b");
  48.         table.addCell("Col c");
  49.         table.addCell("Value a");
  50.         table.addCell("Value b");
  51.         table.addCell("This is a long description for column c. " +
  52.                 "It needs much more space hence we made sure that the third column is wider.");
  53.         doc.add(table);
  54.         doc.add(new Paragraph("With 2 columns:"));
  55.         table = new Table(2);
  56.         table.setMarginTop(5);
  57.         table.addCell("Col a");
  58.         table.addCell("Col b");
  59.         table.addCell("Value a");
  60.         table.addCell("Value b");
  61.         table.addCell(new Cell(1, 2).add("Value b"));
  62.         table.addCell(new Cell(1, 2).add("This is a long description for column c. " +
  63.                 "It needs much more space hence we made sure that the third column is wider."));
  64.         table.addCell("Col a");
  65.         table.addCell("Col b");
  66.         table.addCell("Value a");
  67.         table.addCell("Value b");
  68.         table.addCell(new Cell(1, 2).add("Value b"));
  69.         table.addCell(new Cell(1, 2).add("This is a long description for column c. " +
  70.                 "It needs much more space hence we made sure that the third column is wider."));
  71.         doc.add(table);
  72.  
  73.         doc.close();
  74.     }
  75. }
TableMeasurements.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/34539028
  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.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.layout.property.TextAlignment;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import java.io.File;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28.  
  29. @Category(SampleTest.class)
  30. public class TableMeasurements extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/tables_measurements.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new TableMeasurements().manipulatePdf(DEST);
  37.     }
  38.  
  39.     public static float millimetersToPoints(float value) {
  40.         return (value / 25.4f) * 72f;
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         Document doc = new Document(pdfDoc);
  47.         Table table = new Table(10);
  48.         table.setWidth(millimetersToPoints(100));
  49.         table.addCell(getCell(10));
  50.         table.addCell(getCell(5));
  51.         table.addCell(getCell(3));
  52.         table.addCell(getCell(2));
  53.         table.addCell(getCell(3));
  54.         table.addCell(getCell(5));
  55.         table.addCell(getCell(1));
  56.         doc.add(table);
  57.         doc.close();
  58.     }
  59.  
  60.     private Cell getCell(int cm) {
  61.         Cell cell = new Cell(1, cm);
  62.         Paragraph p = new Paragraph(
  63.                 String.format("%smm", 10 * cm)).setFontSize(8);
  64.         p.setTextAlignment(TextAlignment.CENTER);
  65.         p.setMultipliedLeading(0.5f);
  66.         p.setMarginTop(0);
  67.         cell.add(p);
  68.         return cell;
  69.     }
  70. }
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