Rowspan and splitting

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

31st May 2016
iText PDF

Switch code for this example

SplittingAndRowspan.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/35356847
  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.AreaBreak;
  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.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. import java.io.IOException;
  29.  
  30. @Category(SampleTest.class)
  31. public class SplittingAndRowspan extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/tables/splitting_and_rowspan.pdf";
  33.  
  34.     public static void main(String[] args) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new SplittingAndRowspan().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws IOException {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Document doc = new Document(pdfDoc, new PageSize(300, 160));
  44.  
  45.         doc.add(new Paragraph("Table with setKeepTogether(true):"));
  46.         Table table = new Table(2);
  47.         table.setKeepTogether(true);
  48.         table.setMarginTop(10);
  49.         Cell cell = new Cell(3, 1);
  50.         cell.add("G");
  51.         cell.add("R");
  52.         cell.add("P");
  53.         table.addCell(cell);
  54.         table.addCell("row 1");
  55.         table.addCell("row 2");
  56.         table.addCell("row 3");
  57.         doc.add(table);
  58.  
  59.         doc.add(new AreaBreak());
  60.  
  61.         doc.add(new Paragraph("Table with setKeepTogether(false):"));
  62.         table.setKeepTogether(false);
  63.         doc.add(table);
  64.  
  65.         doc.close();
  66.     }
  67. }
SplittingNestedTable1.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/35356847
  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.AreaBreak;
  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.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 SplittingNestedTable1 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/splitting_nested_table1.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new SplittingNestedTable1().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  42.         Document doc = new Document(pdfDoc, new PageSize(300, 170));
  43.  
  44.         // Note that the sense of KEEP_TOGETHER property is slightly different from late splitting in itext5
  45.         doc.add(new Paragraph("Table with setKeepTogether(true):"));
  46.         Table table = new Table(2);
  47.         table.setKeepTogether(true);
  48.         table.setMarginTop(10);
  49.         Cell cell = new Cell();
  50.         cell.add("G");
  51.         cell.add("R");
  52.         cell.add("O");
  53.         cell.add("U");
  54.         cell.add("P");
  55.         table.addCell(cell);
  56.         Table inner = new Table(1);
  57.         inner.addCell("row 1");
  58.         inner.addCell("row 2");
  59.         inner.addCell("row 3");
  60.         inner.addCell("row 4");
  61.         inner.addCell("row 5");
  62.         cell = new Cell().add(inner);
  63.         cell.setPadding(0);
  64.         table.addCell(cell);
  65.         doc.add(table);
  66.  
  67.         doc.add(new AreaBreak());
  68.  
  69.         doc.add(new Paragraph("Table with setKeepTogether(false):"));
  70.         table.setKeepTogether(false);
  71.         doc.add(table);
  72.  
  73.         doc.close();
  74.     }
  75. }
SplittingNestedTable2.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/35356847
  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.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23. import org.junit.Ignore;
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Ignore
  29. @Category(SampleTest.class)
  30. public class SplittingNestedTable2 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/splitting_nested_table2.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new SplittingNestedTable2().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  42.         // TODO DEVSIX-466
  43.         Document doc = new Document(pdfDoc, new PageSize(300, 120));
  44.         // doc.add(new Paragraph("Table with setKeepTogether(false):"));
  45.         Table table = new Table(2);
  46.         table.setMarginTop(10);
  47.         Cell cell = new Cell().add("GROUPS");
  48.         //cell.setRotationAngle(Math.toRadians(90));
  49. //        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  50. //        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
  51.         table.addCell(cell);
  52.         Table inner = new Table(1);
  53.         inner.addCell("row 1");
  54.         inner.addCell("row 2");
  55.         inner.addCell("row 3");
  56.         inner.addCell("row 4");
  57.         inner.addCell("row 5");
  58.         cell = new Cell().add(inner);
  59. //        cell.setPadding(0);
  60.         table.addCell(cell);
  61.  
  62. //        doc.add(table);
  63. ////        doc.add(new AreaBreak());
  64. //
  65.         doc.add(new Paragraph("T")); // able with setKeepTogether(true):"));
  66. //        table = new Table(2);
  67.         table.setKeepTogether(true);
  68. //        table.setMarginTop(10);
  69. //        cell = new Cell().add("GROUPS");
  70. //        //cell.setRotationAngle(Math.toRadians(90));
  71. //        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  72. //        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
  73. //        table.addCell(cell);
  74. //        inner = new Table(1);
  75. //        inner.addCell("row 1");
  76. //        inner.addCell("row 2");
  77. //        inner.addCell("row 3");
  78. //        inner.addCell("row 4");
  79. //        inner.addCell("row 5");
  80. //        cell = new Cell().add(inner);
  81. //        cell.setPadding(0);
  82. //        table.addCell(cell);
  83. //
  84.         doc.add(table);
  85.  
  86.         doc.close();
  87.     }
  88. }
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