Rowspan and splitting

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

12th February 2016
admin-marketing

Switch code for this example

SplittingAndRowspan.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35356847
  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.Rectangle;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. public class SplittingAndRowspan {
  24.     public static final String DEST = "results/tables/splitting_and_rowspan.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 SplittingAndRowspan().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document(new Rectangle(300, 150));
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         document.add(new Paragraph("Table with setSplitLate(true):"));
  38.         PdfPTable table = new PdfPTable(2);
  39.         table.setSpacingBefore(10);
  40.         PdfPCell cell = new PdfPCell();
  41.         cell.addElement(new Paragraph("G"));
  42.         cell.addElement(new Paragraph("R"));
  43.         cell.addElement(new Paragraph("P"));
  44.         cell.setRowspan(3);
  45.         table.addCell(cell);
  46.         table.addCell("row 1");
  47.         table.addCell("row 2");
  48.         table.addCell("row 3");
  49.         document.add(table);
  50.         document.add(new Paragraph("Table with setSplitLate(false):"));
  51.         table.setSplitLate(false);
  52.         document.add(table);
  53.         document.close();
  54.     }
  55. }
SplittingNestedTable1.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35356847
  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.Rectangle;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. public class SplittingNestedTable1 {
  24.     public static final String DEST = "results/tables/splitting_nested_table1.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 SplittingNestedTable1().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document(new Rectangle(300, 150));
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         document.add(new Paragraph("Table with setSplitLate(true):"));
  38.         PdfPTable table = new PdfPTable(2);
  39.         table.setSpacingBefore(10);
  40.         PdfPCell cell = new PdfPCell();
  41.         cell.addElement(new Paragraph("G"));
  42.         cell.addElement(new Paragraph("R"));
  43.         cell.addElement(new Paragraph("O"));
  44.         cell.addElement(new Paragraph("U"));
  45.         cell.addElement(new Paragraph("P"));
  46.         table.addCell(cell);
  47.         PdfPTable inner = new PdfPTable(1);
  48.         inner.addCell("row 1");
  49.         inner.addCell("row 2");
  50.         inner.addCell("row 3");
  51.         inner.addCell("row 4");
  52.         inner.addCell("row 5");
  53.         cell = new PdfPCell(inner);
  54.         cell.setPadding(0);
  55.         table.addCell(cell);
  56.         document.add(table);
  57.         document.newPage();
  58.         document.add(new Paragraph("Table with setSplitLate(false):"));
  59.         table.setSplitLate(false);
  60.         document.add(table);
  61.         document.close();
  62.     }
  63. }
SplittingNestedTable2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35356847
  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.Paragraph;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.Rectangle;
  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. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. /**
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. public class SplittingNestedTable2 {
  26.     public static final String DEST = "results/tables/splitting_nested_table2.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 SplittingNestedTable2().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document(new Rectangle(300, 150));
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         document.add(new Paragraph("Table with setSplitLate(true):"));
  40.         PdfPTable table = new PdfPTable(2);
  41.         table.setSpacingBefore(10);
  42.         PdfPCell cell = new PdfPCell( new Phrase("GROUPS"));
  43.         cell.setRotation(90);
  44.         cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  45.         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  46.         table.addCell(cell);
  47.         PdfPTable inner = new PdfPTable(1);
  48.         inner.addCell("row 1");
  49.         inner.addCell("row 2");
  50.         inner.addCell("row 3");
  51.         inner.addCell("row 4");
  52.         inner.addCell("row 5");
  53.         cell = new PdfPCell(inner);
  54.         cell.setPadding(0);
  55.         table.addCell(cell);
  56.         document.add(table);
  57.         document.newPage();
  58.         document.add(new Paragraph("Table with setSplitLate(false):"));
  59.         table.setSplitLate(false);
  60.         document.add(table);
  61.         document.close();
  62.     }
  63. }
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