iText 7 Building Blocks - Chapter 5: examples AbstractElement (part 2)

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

3rd August 2016
iText PDF

Switch code for this example

C05E01_MyFirstTable.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.test.annotations.WrapToTest;
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * @author Bruno Lowagie (iText Software)
  27.  */
  28. @WrapToTest
  29. public class C05E01_MyFirstTable {
  30.    
  31.     public static final String DEST = "results/chapter05/my_first_table.pdf";
  32.        
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C05E01_MyFirstTable().createPdf(DEST);
  37.     }
  38.    
  39.     public void createPdf(String dest) throws IOException {
  40.         //Initialize PDF document
  41.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  42.        
  43.         // Initialize document
  44.         Document document = new Document(pdf);
  45.         Table table = new Table(new float[] {1, 1, 1});
  46.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  47.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  48.         table.addCell("row 1; cell 1");
  49.         table.addCell("row 1; cell 2");
  50.         table.addCell("row 2; cell 1");
  51.         table.addCell("row 2; cell 2");
  52.         document.add(table);
  53.         document.close();
  54.     }
  55. }
C05E02_ColumnWidths1.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.test.annotations.WrapToTest;
  22. import java.io.File;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * @author Bruno Lowagie (iText Software)
  27.  */
  28. @WrapToTest
  29. public class C05E02_ColumnWidths1 {
  30.  
  31.     public static final String DEST = "results/chapter05/column_widths1.pdf";
  32.  
  33.     public static void main(String args[]) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new C05E02_ColumnWidths1().createPdf(DEST);
  37.     }
  38.  
  39.     public void createPdf(String dest) throws IOException {
  40.         //Initialize PDF document
  41.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  42.  
  43.         // Initialize document
  44.         Document document = new Document(pdf);
  45.         Table table = new Table(new float[]{200, 100, 100});
  46.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  47.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  48.         table.addCell("row 1; cell 1");
  49.         table.addCell("row 1; cell 2");
  50.         table.addCell("row 2; cell 1");
  51.         table.addCell("row 2; cell 2");
  52.         document.add(table);
  53.         document.close();
  54.     }
  55. }
C05E02_ColumnWidths2.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.UnitValue;
  22. import com.itextpdf.test.annotations.WrapToTest;
  23. import java.io.File;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * @author Bruno Lowagie (iText Software)
  28.  */
  29. @WrapToTest
  30. public class C05E02_ColumnWidths2 {
  31.  
  32.     public static final String DEST = "results/chapter05/column_widths2.pdf";
  33.  
  34.     public static void main(String args[]) throws IOException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new C05E02_ColumnWidths2().createPdf(DEST);
  38.     }
  39.  
  40.     public void createPdf(String dest) throws IOException {
  41.         //Initialize PDF document
  42.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  43.  
  44.         // Initialize document
  45.         Document document = new Document(pdf);
  46.         Table table = new Table(UnitValue.createPercentArray(new float[]{1, 1, 1}));
  47.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  48.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  49.         table.addCell("row 1; cell 1");
  50.         table.addCell("row 1; cell 2");
  51.         table.addCell("row 2; cell 1");
  52.         table.addCell("row 2; cell 2");
  53.         document.add(table);
  54.         document.close();
  55.     }
  56. }
C05E02_ColumnWidths3.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.HorizontalAlignment;
  22. import com.itextpdf.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C05E02_ColumnWidths3 {
  32.  
  33.     public static final String DEST = "results/chapter05/column_widths3.pdf";
  34.  
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C05E02_ColumnWidths3().createPdf(DEST);
  39.     }
  40.  
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.  
  45.         // Initialize document
  46.         Document document = new Document(pdf);
  47.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  48.         table.setWidth(450);
  49.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  50.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  51.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  52.         table.addCell("row 1; cell 1");
  53.         table.addCell("row 1; cell 2");
  54.         table.addCell("row 2; cell 1");
  55.         table.addCell("row 2; cell 2");
  56.         document.add(table);
  57.         document.close();
  58.     }
  59. }
C05E02_ColumnWidths4.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.HorizontalAlignment;
  22. import com.itextpdf.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C05E02_ColumnWidths4 {
  32.    
  33.     public static final String DEST = "results/chapter05/column_widths4.pdf";
  34.        
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C05E02_ColumnWidths4().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.        
  45.         // Initialize document
  46.         Document document = new Document(pdf);
  47.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  48.         table.setWidth(UnitValue.createPercentValue(80));
  49.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  50.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  51.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  52.         table.addCell("row 1; cell 1");
  53.         table.addCell("row 1; cell 2");
  54.         table.addCell("row 2; cell 1");
  55.         table.addCell("row 2; cell 2");
  56.         document.add(table);
  57.         document.close();
  58.     }
  59. }
C05E02_ColumnWidths5.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.HorizontalAlignment;
  22. import com.itextpdf.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C05E02_ColumnWidths5 {
  32.  
  33.     public static final String DEST = "results/chapter05/column_widths5.pdf";
  34.  
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C05E02_ColumnWidths5().createPdf(DEST);
  39.     }
  40.  
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.  
  45.         // Initialize document
  46.         Document document = new Document(pdf);
  47.         Table table = new Table(new float[]{2, 1, 1});
  48.         table.setWidth(UnitValue.createPercentValue(80));
  49.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  50.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  51.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
  52.         table.addCell("row 1; cell 1");
  53.         table.addCell("row 1; cell 2");
  54.         table.addCell("row 2; cell 1");
  55.         table.addCell("row 2; cell 2");
  56.         document.add(table);
  57.         document.close();
  58.     }
  59. }
C05E03_CellAlignment.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.HorizontalAlignment;
  22. import com.itextpdf.layout.property.TextAlignment;
  23. import com.itextpdf.layout.property.UnitValue;
  24. import com.itextpdf.layout.property.VerticalAlignment;
  25. import com.itextpdf.test.annotations.WrapToTest;
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. /**
  30.  * @author Bruno Lowagie (iText Software)
  31.  */
  32. @WrapToTest
  33. public class C05E03_CellAlignment {
  34.    
  35.     public static final String DEST = "results/chapter05/cell_alignment.pdf";
  36.        
  37.     public static void main(String args[]) throws IOException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new C05E03_CellAlignment().createPdf(DEST);
  41.     }
  42.    
  43.     public void createPdf(String dest) throws IOException {
  44.         //Initialize PDF document
  45.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  46.        
  47.         // Initialize document
  48.         Document document = new Document(pdf);
  49.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  50.         table.setWidth(UnitValue.createPercentValue(80));
  51.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  52.         table.setTextAlignment(TextAlignment.CENTER);
  53.         table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
  54.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2"))
  55.             .setTextAlignment(TextAlignment.RIGHT));
  56.         table.addCell("row 1; cell 1");
  57.         table.addCell("row 1; cell 2");
  58.         table.addCell("row 2; cell 1");
  59.         table.addCell("row 2; cell 2");
  60.         Cell cell = new Cell()
  61.             .add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT))
  62.             .add(new Paragraph("Center"))
  63.             .add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT));
  64.         table.addCell(cell);
  65.         cell = new Cell().add(new Paragraph("Middle"))
  66.             .setVerticalAlignment(VerticalAlignment.MIDDLE);
  67.         table.addCell(cell);
  68.         cell = new Cell().add(new Paragraph("Bottom"))
  69.             .setVerticalAlignment(VerticalAlignment.BOTTOM);
  70.         table.addCell(cell);
  71.         document.add(table);
  72.         document.close();
  73.     }
  74. }
C05E04_ColumnHeights.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.borders.DashedBorder;
  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.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C05E04_ColumnHeights {
  32.    
  33.     public static final String DEST = "results/chapter05/column_heights.pdf";
  34.        
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C05E04_ColumnHeights().createPdf(DEST);
  39.     }
  40.    
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.        
  45.         Paragraph p =
  46.             new Paragraph("The Strange Case of\nDr. Jekyll\nand\nMr. Hyde")
  47.             .setBorder(new DashedBorder(0.3f));
  48.        
  49.         // Initialize document
  50.         Document document = new Document(pdf);
  51.         Table table = new Table(UnitValue.createPercentArray(new float[]{1}));
  52.         table.setWidth(UnitValue.createPercentValue(100));
  53.         table.addCell(p);
  54.         Cell cell = new Cell().setHeight(45).add(p);
  55.         table.addCell(cell);
  56.         cell = new Cell().setMinHeight(45).add(p);
  57.         table.addCell(cell);
  58.         cell = new Cell().setMinHeight(135).add(p);
  59.         table.addCell(cell);
  60.         cell = new Cell().setMaxHeight(45).add(p);
  61.         table.addCell(cell);
  62.         cell = new Cell().setMaxHeight(135).add(p);
  63.         table.addCell(cell);
  64.         cell = new Cell().add(p).setRotationAngle(Math.PI / 6);
  65.         table.addCell(cell);
  66.         document.add(table);
  67.         document.close();
  68.     }
  69. }
C05E05_CellPadding.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.kernel.colors.ColorConstants;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  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.layout.property.HorizontalAlignment;
  23. import com.itextpdf.layout.property.UnitValue;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25. import java.io.File;
  26. import java.io.IOException;
  27.  
  28. /**
  29.  * @author Bruno Lowagie (iText Software)
  30.  */
  31. @WrapToTest
  32. public class C05E05_CellPadding {
  33.  
  34.     public static final String DEST = "results/chapter05/cell_padding.pdf";
  35.  
  36.     public static void main(String args[]) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new C05E05_CellPadding().createPdf(DEST);
  40.     }
  41.  
  42.     public void createPdf(String dest) throws IOException {
  43.         //Initialize PDF document
  44.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  45.  
  46.         // Initialize document
  47.         Document document = new Document(pdf);
  48.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  49.         table.setBackgroundColor(ColorConstants.ORANGE);
  50.         table.setWidth(UnitValue.createPercentValue(80));
  51.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  52.         table.addCell(
  53.             new Cell(1, 3).add(new Paragraph("Cell with colspan 3"))
  54.                 .setPadding(10).setBackgroundColor(ColorConstants.GREEN));
  55.         table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2"))
  56.             .setPaddingLeft(30)
  57.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.BLUE));
  58.         table.addCell(new Cell().add(new Paragraph("row 1; cell 1"))
  59.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  60.         table.addCell(new Cell().add(new Paragraph("row 1; cell 2")));
  61.         table.addCell(new Cell().add(new Paragraph("row 2; cell 1"))
  62.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  63.         table.addCell(new Cell().add(new Paragraph("row 2; cell 2")).setPadding(10)
  64.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  65.         document.add(table);
  66.         document.close();
  67.     }
  68. }
C05E05_CellPaddingMargin.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.kernel.colors.ColorConstants;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.property.HorizontalAlignment;
  24. import com.itextpdf.layout.property.UnitValue;
  25. import com.itextpdf.layout.renderer.CellRenderer;
  26. import com.itextpdf.layout.renderer.IRenderer;
  27. import com.itextpdf.test.annotations.WrapToTest;
  28. import java.io.File;
  29. import java.io.IOException;
  30.  
  31. /**
  32.  * @author Bruno Lowagie (iText Software)
  33.  */
  34. @WrapToTest
  35. public class C05E05_CellPaddingMargin {
  36.  
  37.     public static final String DEST = "results/chapter05/cell_margin_padding.pdf";
  38.  
  39.     private class MarginCellRenderer extends CellRenderer {
  40.         public MarginCellRenderer(Cell modelElement) {
  41.             super(modelElement);
  42.         }
  43.  
  44.         @Override
  45.         public IRenderer getNextRenderer() {
  46.             return new MarginCellRenderer((Cell)getModelElement());
  47.         }
  48.  
  49.         @Override
  50.         protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
  51.             return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
  52.         }
  53.     }
  54.  
  55.     private class MarginCell extends Cell {
  56.         public MarginCell() {
  57.             super();
  58.         }
  59.  
  60.         public MarginCell(int rowspan, int colspan) {
  61.             super(rowspan, colspan);
  62.         }
  63.  
  64.         @Override
  65.         protected IRenderer makeNewRenderer() {
  66.             return new MarginCellRenderer(this);
  67.         }
  68.     }
  69.  
  70.     public static void main(String args[]) throws IOException {
  71.         File file = new File(DEST);
  72.         file.getParentFile().mkdirs();
  73.         new C05E05_CellPaddingMargin().createPdf(DEST);
  74.     }
  75.  
  76.     public void createPdf(String dest) throws IOException {
  77.         //Initialize PDF document
  78.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  79.  
  80.         // Initialize document
  81.         Document document = new Document(pdf);
  82.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  83.         table.setBackgroundColor(ColorConstants.ORANGE);
  84.         table.setWidth(UnitValue.createPercentValue(80));
  85.         table.setHorizontalAlignment(HorizontalAlignment.CENTER);
  86.         table.addCell(
  87.             new MarginCell(1, 3).add(new Paragraph("Cell with colspan 3"))
  88.                 .setPadding(10).setMargin(5).setBackgroundColor(ColorConstants.GREEN));
  89.         table.addCell(new MarginCell(2, 1).add(new Paragraph("Cell with rowspan 2"))
  90.             .setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
  91.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.BLUE));
  92.         table.addCell(new MarginCell().add(new Paragraph("row 1; cell 1"))
  93.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  94.         table.addCell(new MarginCell().add(new Paragraph("row 1; cell 2")));
  95.         table.addCell(new MarginCell().add(new Paragraph("row 2; cell 1")).setMargin(10)
  96.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  97.         table.addCell(new MarginCell().add(new Paragraph("row 2; cell 2")).setPadding(10)
  98.             .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
  99.         document.add(table);
  100.         document.close();
  101.     }
  102. }
C05E06_CellBorders1.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. import com.itextpdf.kernel.colors.ColorConstants;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.borders.DashedBorder;
  23. import com.itextpdf.layout.borders.DottedBorder;
  24. import com.itextpdf.layout.borders.SolidBorder;
  25. import com.itextpdf.layout.element.Cell;
  26. import com.itextpdf.layout.element.Paragraph;
  27. import com.itextpdf.layout.element.Table;
  28. import com.itextpdf.layout.property.HorizontalAlignment;
  29. import com.itextpdf.layout.property.TextAlignment;
  30. import com.itextpdf.layout.property.UnitValue;
  31. import com.itextpdf.layout.property.VerticalAlignment;
  32. import com.itextpdf.test.annotations.WrapToTest;
  33.  
  34. /**
  35.  * @author Bruno Lowagie (iText Software)
  36.  */
  37. @WrapToTest
  38. public class C05E06_CellBorders1 {
  39.  
  40.     public static final String DEST = "results/chapter05/cell_borders1.pdf";
  41.  
  42.     public static void main(String args[]) throws IOException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new C05E06_CellBorders1().createPdf(DEST);
  46.     }
  47.  
  48.     public void createPdf(String dest) throws IOException {
  49.         //Initialize PDF document
  50.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  51.  
  52.         // Initialize document
  53.         Document document = new Document(pdf);
  54.  
  55.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  56.         table.setWidth(UnitValue.createPercentValue(80))
  57.             .setHorizontalAlignment(HorizontalAlignment.CENTER)
  58.             .setTextAlignment(TextAlignment.CENTER);
  59.         table.addCell(new Cell(1, 3)
  60.             .add(new Paragraph("Cell with colspan 3"))
  61.             .setVerticalAlignment(VerticalAlignment.MIDDLE)
  62.             .setBorder(new DashedBorder(0.5f)));
  63.         table.addCell(new Cell(2, 1)
  64.             .add(new Paragraph("Cell with rowspan 2"))
  65.             .setVerticalAlignment(VerticalAlignment.MIDDLE)
  66.             .setBorderBottom(new DottedBorder(0.5f))
  67.             .setBorderLeft(new DottedBorder(0.5f)));
  68.         table.addCell(new Cell()
  69.             .add(new Paragraph("row 1; cell 1"))
  70.             .setBorder(new DottedBorder(ColorConstants.ORANGE, 0.5f)));
  71.         table.addCell(new Cell()
  72.             .add(new Paragraph("row 1; cell 2")));
  73.         table.addCell(new Cell()
  74.             .add(new Paragraph("row 2; cell 1"))
  75.             .setBorderBottom(new SolidBorder(2)));
  76.         table.addCell(new Cell()
  77.             .add(new Paragraph("row 2; cell 2"))
  78.             .setBorderBottom(new SolidBorder(2)));
  79.         document.add(table);
  80.  
  81.         document.close();
  82.     }
  83. }
C05E06_CellBorders2.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  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.borders.Border;
  22. import com.itextpdf.layout.borders.SolidBorder;
  23. import com.itextpdf.layout.element.Cell;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.property.HorizontalAlignment;
  27. import com.itextpdf.layout.property.TextAlignment;
  28. import com.itextpdf.layout.property.UnitValue;
  29. import com.itextpdf.layout.property.VerticalAlignment;
  30. import com.itextpdf.test.annotations.WrapToTest;
  31.  
  32. /**
  33.  * @author Bruno Lowagie (iText Software)
  34.  */
  35. @WrapToTest
  36. public class C05E06_CellBorders2 {
  37.  
  38.     public static final String DEST = "results/chapter05/cell_borders2.pdf";
  39.  
  40.     public static void main(String args[]) throws IOException {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new C05E06_CellBorders2().createPdf(DEST);
  44.     }
  45.  
  46.     public void createPdf(String dest) throws IOException {
  47.         //Initialize PDF document
  48.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  49.  
  50.         // Initialize document
  51.         Document document = new Document(pdf);
  52.  
  53.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  54.         table.setBorder(new SolidBorder(3))
  55.             .setWidth(UnitValue.createPercentValue(80))
  56.             .setHorizontalAlignment(HorizontalAlignment.CENTER)
  57.             .setTextAlignment(TextAlignment.CENTER);
  58.         table.addCell(new Cell(1, 3)
  59.             .add(new Paragraph("Cell with colspan 3"))
  60.             .setVerticalAlignment(VerticalAlignment.MIDDLE)
  61.             .setBorder(Border.NO_BORDER));
  62.         table.addCell(new Cell(2, 1)
  63.             .add(new Paragraph("Cell with rowspan 2"))
  64.             .setVerticalAlignment(VerticalAlignment.MIDDLE)
  65.             .setBorder(Border.NO_BORDER));
  66.         table.addCell(new Cell().add(new Paragraph("row 1; cell 1")));
  67.         table.addCell(new Cell().add(new Paragraph("row 1; cell 2")));
  68.         table.addCell(new Cell().add(new Paragraph("row 2; cell 1")));
  69.         table.addCell(new Cell().add(new Paragraph("row 2; cell 2")));
  70.         document.add(table);
  71.  
  72.         document.close();
  73.     }
  74. }
C05E06_CellBorders3.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.borders.Border;
  24. import com.itextpdf.layout.element.Cell;
  25. import com.itextpdf.layout.element.Paragraph;
  26. import com.itextpdf.layout.element.Table;
  27. import com.itextpdf.layout.property.HorizontalAlignment;
  28. import com.itextpdf.layout.property.TextAlignment;
  29. import com.itextpdf.layout.property.UnitValue;
  30. import com.itextpdf.layout.property.VerticalAlignment;
  31. import com.itextpdf.layout.renderer.CellRenderer;
  32. import com.itextpdf.layout.renderer.DrawContext;
  33. import com.itextpdf.layout.renderer.IRenderer;
  34. import com.itextpdf.test.annotations.WrapToTest;
  35.  
  36. /**
  37.  * @author Bruno Lowagie (iText Software)
  38.  */
  39. @WrapToTest
  40. public class C05E06_CellBorders3 {
  41.  
  42.     private class RoundedCornersCellRenderer extends CellRenderer {
  43.         public RoundedCornersCellRenderer(Cell modelElement) {
  44.             super(modelElement);
  45.         }
  46.  
  47.         @Override
  48.         public void drawBorder(DrawContext drawContext) {
  49.             Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
  50.             UnitValue[] margins = getMargins();
  51.             Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
  52.             PdfCanvas canvas = drawContext.getCanvas();
  53.             canvas.roundRectangle(rectangle.getX(), rectangle.getY(),
  54.                 rectangle.getWidth(), rectangle.getHeight(), 5).stroke();
  55.             super.drawBorder(drawContext);
  56.         }
  57.  
  58.         @Override
  59.         public IRenderer getNextRenderer() {
  60.             return new RoundedCornersCellRenderer((Cell)getModelElement());
  61.         }
  62.  
  63.         @Override
  64.         protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
  65.             return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
  66.         }
  67.     }
  68.  
  69.     private class RoundedCornersCell extends Cell {
  70.         public RoundedCornersCell() {
  71.             super();
  72.             setBorder(Border.NO_BORDER);
  73.             setMargin(2);
  74.         }
  75.  
  76.         public RoundedCornersCell(int rowspan, int colspan) {
  77.             super(rowspan, colspan);
  78.             setBorder(Border.NO_BORDER);
  79.             setVerticalAlignment(VerticalAlignment.MIDDLE);
  80.             setMargin(5);
  81.         }
  82.  
  83.         @Override
  84.         protected IRenderer makeNewRenderer() {
  85.             return new RoundedCornersCellRenderer(this);
  86.         }
  87.     }
  88.  
  89.     public static final String DEST = "results/chapter05/cell_borders3.pdf";
  90.  
  91.     public static void main(String args[]) throws IOException {
  92.         File file = new File(DEST);
  93.         file.getParentFile().mkdirs();
  94.         new C05E06_CellBorders3().createPdf(DEST);
  95.     }
  96.  
  97.     public void createPdf(String dest) throws IOException {
  98.         //Initialize PDF document
  99.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  100.  
  101.         // Initialize document
  102.         Document document = new Document(pdf);
  103.  
  104.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
  105.         table.setWidth(UnitValue.createPercentValue(80))
  106.             .setHorizontalAlignment(HorizontalAlignment.CENTER)
  107.             .setTextAlignment(TextAlignment.CENTER);
  108.         Cell cell = new RoundedCornersCell(1, 3)
  109.             .add(new Paragraph("Cell with colspan 3"));
  110.         table.addCell(cell);
  111.         cell = new RoundedCornersCell(2, 1)
  112.             .add(new Paragraph("Cell with rowspan 2"));
  113.         table.addCell(cell);
  114.         cell = new RoundedCornersCell()
  115.             .add(new Paragraph("row 1; cell 1"));
  116.         table.addCell(cell);
  117.         cell = new RoundedCornersCell()
  118.             .add(new Paragraph("row 1; cell 2"));
  119.         table.addCell(cell);
  120.         cell = new RoundedCornersCell()
  121.             .add(new Paragraph("row 2; cell 1"));
  122.         table.addCell(cell);
  123.         cell = new RoundedCornersCell()
  124.             .add(new Paragraph("row 2; cell 2"));
  125.         table.addCell(cell);
  126.         document.add(table);
  127.  
  128.         document.close();
  129.     }
  130. }
C05E06_CellBorders4.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Cell;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.property.HorizontalAlignment;
  27. import com.itextpdf.layout.property.TextAlignment;
  28. import com.itextpdf.layout.property.UnitValue;
  29. import com.itextpdf.layout.renderer.CellRenderer;
  30. import com.itextpdf.layout.renderer.DrawContext;
  31. import com.itextpdf.layout.renderer.IRenderer;
  32. import com.itextpdf.layout.renderer.TableRenderer;
  33. import com.itextpdf.test.annotations.WrapToTest;
  34.  
  35. /**
  36.  * @author Bruno Lowagie (iText Software)
  37.  */
  38. @WrapToTest
  39. public class C05E06_CellBorders4 {
  40.  
  41.     private class RoundedCornersTableRenderer extends TableRenderer {
  42.  
  43.         public RoundedCornersTableRenderer(Table modelElement) {
  44.             super(modelElement);
  45.         }
  46.  
  47.         @Override
  48.         public IRenderer getNextRenderer() {
  49.             return new RoundedCornersTableRenderer((Table)getModelElement());
  50.         }
  51.  
  52.         @Override
  53.         protected void drawBorders(DrawContext drawContext) {
  54.             Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
  55.             UnitValue[] margins = getMargins();
  56.             Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
  57.             PdfCanvas canvas = drawContext.getCanvas();
  58.             canvas.roundRectangle(rectangle.getX() + 1, rectangle.getY() + 1,
  59.                     rectangle.getWidth() - 2, rectangle.getHeight() -2, 5).stroke();
  60.             super.drawBorder(drawContext);
  61.         }
  62.  
  63.     }
  64.  
  65.     private class RoundedCornersCellRenderer extends CellRenderer {
  66.         public RoundedCornersCellRenderer(Cell modelElement) {
  67.             super(modelElement);
  68.         }
  69.  
  70.         @Override
  71.         public void drawBorder(DrawContext drawContext) {
  72.             Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
  73.             UnitValue[] margins = getMargins();
  74.             Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
  75.             PdfCanvas canvas = drawContext.getCanvas();
  76.             canvas.roundRectangle(rectangle.getX() + 1, rectangle.getY() + 1,
  77.                     rectangle.getWidth() - 2, rectangle.getHeight() -2, 5).stroke();
  78.             super.drawBorder(drawContext);
  79.         }
  80.  
  81.         @Override
  82.         public IRenderer getNextRenderer() {
  83.             return new RoundedCornersCellRenderer((Cell)getModelElement());
  84.         }
  85.  
  86.         @Override
  87.         protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
  88.             return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
  89.         }
  90.     }
  91.  
  92.     private class RoundedCornersCell extends Cell {
  93.         public RoundedCornersCell() {
  94.             super();
  95.             setMargin(2);
  96.         }
  97.  
  98.         public RoundedCornersCell(int rowspan, int colspan) {
  99.             super(rowspan, colspan);
  100.             setMargin(2);
  101.         }
  102.  
  103.         @Override
  104.         protected IRenderer makeNewRenderer() {
  105.             return new RoundedCornersCellRenderer(this);
  106.         }
  107.     }
  108.  
  109.     public static final String DEST = "results/chapter05/cell_borders4.pdf";
  110.  
  111.     public static void main(String args[]) throws IOException {
  112.         File file = new File(DEST);
  113.         file.getParentFile().mkdirs();
  114.         new C05E06_CellBorders4().createPdf(DEST);
  115.     }
  116.  
  117.     public void createPdf(String dest) throws IOException {
  118.         //Initialize PDF document
  119.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  120.  
  121.         // Initialize document
  122.         Document document = new Document(pdf);
  123.  
  124.         Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}))
  125.             .setWidth(UnitValue.createPercentValue(80))
  126.             .setHorizontalAlignment(HorizontalAlignment.CENTER)
  127.             .setTextAlignment(TextAlignment.CENTER);
  128.         Cell cell = new RoundedCornersCell(1, 3)
  129.             .add(new Paragraph("Cell with colspan 3"));
  130.         table.addCell(cell);
  131.         cell = new RoundedCornersCell(2, 1)
  132.             .add(new Paragraph("Cell with rowspan 2"));
  133.         table.addCell(cell);
  134.         cell = new RoundedCornersCell()
  135.             .add(new Paragraph("row 1; cell 1"));
  136.         table.addCell(cell);
  137.         cell = new RoundedCornersCell()
  138.             .add(new Paragraph("row 1; cell 2"));
  139.         table.addCell(cell);
  140.         cell = new RoundedCornersCell()
  141.             .add(new Paragraph("row 2; cell 1"));
  142.         table.addCell(cell);
  143.         cell = new RoundedCornersCell()
  144.             .add(new Paragraph("row 2; cell 2"));
  145.         table.addCell(cell);
  146.         table.setNextRenderer(new RoundedCornersTableRenderer(table));
  147.         document.add(table);
  148.  
  149.         document.close();
  150.     }
  151. }
C05E07_NestedTable.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.layout.property.HorizontalAlignment;
  22. import com.itextpdf.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class C05E07_NestedTable {
  32.  
  33.     public static final String DEST = "results/chapter05/nested_tables.pdf";
  34.  
  35.     public static void main(String args[]) throws IOException {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new C05E07_NestedTable().createPdf(DEST);
  39.     }
  40.  
  41.     public void createPdf(String dest) throws IOException {
  42.         //Initialize PDF document
  43.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  44.  
  45.         // Initialize document
  46.         Document document = new Document(pdf);
  47.         Table table = new Table(new float[]{1, 1})
  48.             .setWidth(UnitValue.createPercentValue(80))
  49.             .setHorizontalAlignment(HorizontalAlignment.CENTER);
  50.         table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
  51.         table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
  52.         Table inner = new Table(new float[]{1, 1});
  53.         inner.addCell("row 1; cell 1");
  54.         inner.addCell("row 1; cell 2");
  55.         inner.addCell("row 2; cell 1");
  56.         inner.addCell("row 2; cell 2");
  57.         table.addCell(inner);
  58.         document.add(table);
  59.  
  60.         table = new Table(new float[]{1, 1})
  61.             .setMarginTop(10)
  62.             .setWidth(UnitValue.createPercentValue(80))
  63.             .setHorizontalAlignment(HorizontalAlignment.CENTER);
  64.         table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
  65.         table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
  66.         inner = new Table(new float[]{1, 1});
  67.         inner.addCell("row 1; cell 1");
  68.         inner.addCell("row 1; cell 2");
  69.         inner.addCell("row 2; cell 1");
  70.         inner.addCell("row 2; cell 2");
  71.         table.addCell(new Cell().add(inner).setPadding(0));
  72.         document.add(table);
  73.  
  74.         table = new Table(new float[]{1, 1})
  75.             .setMarginTop(10)
  76.             .setWidth(UnitValue.createPercentValue(80))
  77.             .setHorizontalAlignment(HorizontalAlignment.CENTER);
  78.         table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
  79.         table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
  80.         inner = new Table(new float[]{1, 1})
  81.             .setWidth(UnitValue.createPercentValue(100));
  82.         inner.addCell("row 1; cell 1");
  83.         inner.addCell("row 1; cell 2");
  84.         inner.addCell("row 2; cell 1");
  85.         inner.addCell("row 2; cell 2");
  86.         table.addCell(new Cell().add(inner).setPadding(0));
  87.         document.add(table);
  88.  
  89.         document.close();
  90.     }
  91. }
C05E08_JekyllHydeTableV1.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.property.UnitValue;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.List;
  29.  
  30. /**
  31.  * @author iText
  32.  */
  33. @WrapToTest
  34. public class C05E08_JekyllHydeTableV1 {
  35.    
  36.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  37.     public static final String DEST = "results/chapter05/jekyll_hyde_table1.pdf";
  38.        
  39.     public static void main(String args[]) throws IOException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new C05E08_JekyllHydeTableV1().createPdf(DEST);
  43.     }
  44.    
  45.     public void createPdf(String dest) throws IOException {
  46.         //Initialize PDF document
  47.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  48.        
  49.         // Initialize document
  50.         Document document = new Document(pdf, PageSize.A4.rotate());
  51.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
  52.         table.setWidth(UnitValue.createPercentValue(100));
  53.         List resultSet = CsvTo2DList.convert(SRC, "|");
  54.         List header = resultSet.remove(0);
  55.         for (String field : header) {
  56.             table.addHeaderCell(field);
  57.         }
  58.         Cell cell = new Cell(1, 6).add(new Paragraph("Continued on next page..."));
  59.         table.addFooterCell(cell)
  60.             .setSkipLastFooter(true);
  61.         for (List record : resultSet) {
  62.             for (String field : record) {
  63.                 table.addCell(field);
  64.             }
  65.         }
  66.         document.add(table);
  67.         document.close();
  68.     }
  69. }
C05E09_JekyllHydeTableV2.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.property.UnitValue;
  23. import com.itextpdf.test.annotations.WrapToTest;
  24.  
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.util.List;
  28.  
  29. /**
  30.  * @author iText
  31.  */
  32. @WrapToTest
  33. public class C05E09_JekyllHydeTableV2 {
  34.    
  35.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  36.     public static final String DEST = "results/chapter05/jekyll_hyde_table2.pdf";
  37.        
  38.     public static void main(String args[]) throws IOException {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new C05E09_JekyllHydeTableV2().createPdf(DEST);
  42.     }
  43.    
  44.     public void createPdf(String dest) throws IOException {
  45.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  46.         Document document = new Document(pdf, PageSize.A4.rotate());
  47.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
  48.         table.setWidth(UnitValue.createPercentValue(100));
  49.         List resultSet = CsvTo2DList.convert(SRC, "|");
  50.         List header = resultSet.remove(0);
  51.         for (String field : header) {
  52.             table.addHeaderCell(field);
  53.         }
  54.         for (List record : resultSet) {
  55.             for (String field : record) {
  56.                 table.addCell(field);
  57.             }
  58.         }
  59.         Table outerTable = new Table(new float[]{1})
  60.             .addHeaderCell("Continued from previous page:")
  61.             .setSkipFirstHeader(true)
  62.             .addCell(new Cell().add(table).setPadding(0));
  63.         document.add(outerTable);
  64.         document.close();
  65.     }
  66. }
C05E10_JekyllHydeTableV3.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.io.image.ImageDataFactory;
  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.element.Cell;
  22. import com.itextpdf.layout.element.Image;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.layout.property.UnitValue;
  26. import com.itextpdf.test.annotations.WrapToTest;
  27.  
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.List;
  31.  
  32. /**
  33.  * @author iText
  34.  */
  35. @WrapToTest
  36. public class C05E10_JekyllHydeTableV3 {
  37.    
  38.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  39.     public static final String DEST = "results/chapter05/jekyll_hyde_table3.pdf";
  40.        
  41.     public static void main(String args[]) throws IOException {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new C05E10_JekyllHydeTableV3().createPdf(DEST);
  45.     }
  46.    
  47.     public void createPdf(String dest) throws IOException {
  48.         //Initialize PDF document
  49.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  50.        
  51.         // Initialize document
  52.         Document document = new Document(pdf, PageSize.A4.rotate());
  53.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
  54.         List resultSet = CsvTo2DList.convert(SRC, "|");
  55.         List header = resultSet.remove(0);
  56.         for (String field : header) {
  57.             table.addHeaderCell(field);
  58.         }
  59.         Cell cell;
  60.         for (List record : resultSet) {
  61.             cell = new Cell();
  62.             File file = new File(String.format(
  63.                 "src/main/resources/img/%s.jpg", record.get(0)));
  64.             if (file.exists()) {
  65.                 Image img = new Image(ImageDataFactory.create(file.getPath()));
  66.                 img.setAutoScaleWidth(true);
  67.                 img.setWidth(UnitValue.createPercentValue(100));
  68.                 cell.add(img);
  69.             }
  70.             else {
  71.                 cell.add(new Paragraph(record.get(0)));
  72.             }
  73.             table.addCell(cell);
  74.             table.addCell(record.get(1));
  75.             table.addCell(record.get(2));
  76.             table.addCell(record.get(3));
  77.             table.addCell(record.get(4));
  78.             table.addCell(record.get(5));
  79.         }
  80.         document.add(table);
  81.         document.close();
  82.     }
  83. }
C05E11_JekyllHydeTableV4.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.property.UnitValue;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.List;
  29.  
  30. /**
  31.  * @author iText
  32.  */
  33. @WrapToTest
  34. public class C05E11_JekyllHydeTableV4 {
  35.    
  36.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  37.     public static final String DEST = "results/chapter05/jekyll_hyde_table4.pdf";
  38.        
  39.     public static void main(String args[]) throws IOException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new C05E11_JekyllHydeTableV4().createPdf(DEST);
  43.     }
  44.    
  45.     public void createPdf(String dest) throws IOException {
  46.         //Initialize PDF document
  47.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  48.        
  49.         // Initialize document
  50.         Document document = new Document(pdf, PageSize.A4.rotate());
  51.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 32}));
  52.         table.setWidth(UnitValue.createPercentValue(100));
  53.         List resultSet = CsvTo2DList.convert(SRC, "|");
  54.         resultSet.remove(0);
  55.         table.addHeaderCell("imdb")
  56.             .addHeaderCell("Information about the movie");
  57.         Cell cell;
  58.         for (List record : resultSet) {
  59.             table.addCell(record.get(0));
  60.             cell = new Cell()
  61.                 .add(new Paragraph(record.get(1)))
  62.                 .add(new Paragraph(record.get(2)))
  63.                 .add(new Paragraph(record.get(3)))
  64.                 .add(new Paragraph(record.get(4)))
  65.                 .add(new Paragraph(record.get(5)));
  66.             table.addCell(cell);
  67.         }
  68.         document.add(table);
  69.         document.close();
  70.     }
  71. }
C05E12_JekyllHydeTableV5.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.property.UnitValue;
  24. import com.itextpdf.test.annotations.WrapToTest;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.List;
  29.  
  30. /**
  31.  * @author iText
  32.  */
  33. @WrapToTest
  34. public class C05E12_JekyllHydeTableV5 {
  35.    
  36.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  37.     public static final String DEST = "results/chapter05/jekyll_hyde_table5.pdf";
  38.        
  39.     public static void main(String args[]) throws IOException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new C05E12_JekyllHydeTableV5().createPdf(DEST);
  43.     }
  44.    
  45.     public void createPdf(String dest) throws IOException {
  46.         //Initialize PDF document
  47.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  48.        
  49.         // Initialize document
  50.         Document document = new Document(pdf, PageSize.A4.rotate());
  51.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 32}));
  52.         table.setWidth(UnitValue.createPercentValue(100));
  53.         List resultSet = CsvTo2DList.convert(SRC, "|");
  54.         resultSet.remove(0);
  55.         table.addHeaderCell("imdb")
  56.             .addHeaderCell("Information about the movie");
  57.         Cell cell;
  58.         for (List record : resultSet) {
  59.             table.addCell(record.get(0));
  60.             cell = new Cell()
  61.                 .add(new Paragraph(record.get(1)))
  62.                 .add(new Paragraph(record.get(2)))
  63.                 .add(new Paragraph(record.get(3)))
  64.                 .add(new Paragraph(record.get(4)))
  65.                 .add(new Paragraph(record.get(5)));
  66.             cell.setKeepTogether(true);
  67.             table.addCell(cell);
  68.         }
  69.         document.add(table);
  70.         document.close();
  71.     }
  72. }
C05E13_JekyllHydeTableV6.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.colors.ColorConstants;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.property.UnitValue;
  25. import com.itextpdf.layout.renderer.CellRenderer;
  26. import com.itextpdf.layout.renderer.DrawContext;
  27. import com.itextpdf.layout.renderer.TableRenderer;
  28. import com.itextpdf.test.annotations.WrapToTest;
  29.  
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.util.List;
  33.  
  34. /**
  35.  * @author iText
  36.  */
  37. @WrapToTest
  38. public class C05E13_JekyllHydeTableV6 {
  39.  
  40.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  41.     public static final String DEST = "results/chapter05/jekyll_hyde_table6"
  42.             + ".pdf";
  43.  
  44.     public static void main(String args[]) throws IOException {
  45.         File file = new File(DEST);
  46.         file.getParentFile().mkdirs();
  47.         new C05E13_JekyllHydeTableV6().createPdf(DEST);
  48.     }
  49.  
  50.     public void createPdf(String dest) throws IOException {
  51.         //Initialize PDF document
  52.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  53.  
  54.         // Initialize document
  55.         Document document = new Document(pdf, PageSize.A4.rotate());
  56.         List resultSet = CsvTo2DList.convert(SRC, "|");
  57.         List header = resultSet.remove(0);
  58.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
  59.         table.setWidth(UnitValue.createPercentValue(100));
  60.         int nRows = resultSet.size();
  61.         table.setNextRenderer(new AlternatingBackgroundTableRenderer(
  62.             table, new Table.RowRange(0, nRows - 1)));
  63.         for (String field : header) {
  64.             table.addHeaderCell(field);
  65.         }
  66.         for (List record : resultSet) {
  67.             for (String field : record) {
  68.                 table.addCell(field);
  69.             }
  70.         }
  71.         document.add(table);
  72.         document.close();
  73.     }
  74. }
  75.  
  76. class AlternatingBackgroundTableRenderer extends TableRenderer {
  77.     private boolean isOdd = true;
  78.  
  79.     public AlternatingBackgroundTableRenderer(Table modelElement, Table.RowRange rowRange) {
  80.         super(modelElement, rowRange);
  81.     }
  82.  
  83.     public AlternatingBackgroundTableRenderer(Table modelElement) {
  84.         super(modelElement);
  85.     }
  86.  
  87.     @Override
  88.     public AlternatingBackgroundTableRenderer getNextRenderer() {
  89.         return new AlternatingBackgroundTableRenderer((Table) modelElement);
  90.     }
  91.  
  92.     @Override
  93.     public void draw(DrawContext drawContext) {
  94.         for (int i = 0; i < rows.size() && null != rows.get(i) && null != rows.get(i)[0]; i++) {
  95.             CellRenderer[] renderers = rows.get(i);
  96.             Rectangle leftCell =
  97.                 renderers[0].getOccupiedAreaBBox();
  98.             Rectangle rightCell =
  99.                 renderers[renderers.length - 1].getOccupiedAreaBBox();
  100.             Rectangle rect = new Rectangle(
  101.                 leftCell.getLeft(), leftCell.getBottom(),
  102.                 rightCell.getRight() - leftCell.getLeft(),
  103.                 leftCell.getHeight());
  104.             PdfCanvas canvas = drawContext.getCanvas();
  105.             canvas.saveState();
  106.             if (isOdd) {
  107.                 canvas.setFillColor(ColorConstants.LIGHT_GRAY);
  108.                 isOdd = false;
  109.             } else {
  110.                 canvas.setFillColor(ColorConstants.YELLOW);
  111.                 isOdd = true;
  112.             }
  113.             canvas.rectangle(rect);
  114.             canvas.fill();
  115.             canvas.restoreState();
  116.         }
  117.         super.draw(drawContext);
  118.     }
  119. }
C05E14_JekyllHydeTableV7.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  15. import com.itextpdf.highlevel.util.CsvTo2DList;
  16. import com.itextpdf.kernel.colors.ColorConstants;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Cell;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.property.UnitValue;
  27. import com.itextpdf.layout.renderer.CellRenderer;
  28. import com.itextpdf.layout.renderer.DrawContext;
  29. import com.itextpdf.layout.renderer.IRenderer;
  30. import com.itextpdf.test.annotations.WrapToTest;
  31.  
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.util.List;
  35.  
  36. /**
  37.  * @author iText
  38.  */
  39. @WrapToTest
  40. public class C05E14_JekyllHydeTableV7 {
  41.  
  42.     public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
  43.     public static final String DEST = "results/chapter05/jekyll_hyde_table7.pdf";
  44.  
  45.     public static void main(String args[]) throws IOException {
  46.         File file = new File(DEST);
  47.         file.getParentFile().mkdirs();
  48.         new C05E14_JekyllHydeTableV7().createPdf(DEST);
  49.     }
  50.  
  51.     public void createPdf(String dest) throws IOException {
  52.         //Initialize PDF document
  53.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  54.  
  55.         // Initialize document
  56.         Document document = new Document(pdf, PageSize.A4.rotate());
  57.         Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
  58.         table.setWidth(UnitValue.createPercentValue(100));
  59.         List resultSet = CsvTo2DList.convert(SRC, "|");
  60.         List header = resultSet.remove(0);
  61.         for (String field : header) {
  62.             table.addHeaderCell(field);
  63.         }
  64.         for (List record : resultSet) {
  65.             table.addCell(record.get(0));
  66.             table.addCell(record.get(1));
  67.             Cell cell = new Cell().add(new Paragraph(record.get(2)));
  68.             cell.setNextRenderer(new RunlengthRenderer(cell, record.get(5)));
  69.             table.addCell(cell);
  70.             table.addCell(record.get(3));
  71.             table.addCell(record.get(4));
  72.             table.addCell(record.get(5));
  73.         }
  74.         document.add(table);
  75.         document.close();
  76.     }
  77.  
  78.     private class RunlengthRenderer extends CellRenderer {
  79.         private int runlength;
  80.  
  81.         public RunlengthRenderer(Cell modelElement, String duration) {
  82.             super(modelElement);
  83.             if (duration.trim().isEmpty()) runlength = 0;
  84.             else runlength = Integer.parseInt(duration);
  85.         }
  86.  
  87.         @Override
  88.         public IRenderer getNextRenderer() {
  89.             return new RunlengthRenderer((Cell) getModelElement(), String.valueOf(runlength));
  90.         }
  91.  
  92.         @Override
  93.         public void drawBackground(DrawContext drawContext) {
  94.             if (runlength == 0) return;
  95.             PdfCanvas canvas = drawContext.getCanvas();
  96.             canvas.saveState();
  97.             if (runlength < 90) {
  98.                 canvas.setFillColor(ColorConstants.GREEN);
  99.             } else if (runlength > 240) {
  100.                 runlength = 240;
  101.                 canvas.setFillColor(ColorConstants.RED);
  102.             } else {
  103.                 canvas.setFillColor(ColorConstants.ORANGE);
  104.             }
  105.             Rectangle rect = getOccupiedAreaBBox();
  106.             canvas.rectangle(rect.getLeft(), rect.getBottom(),
  107.                     rect.getWidth() * runlength / 240, rect.getHeight());
  108.             canvas.fill();
  109.             canvas.restoreState();
  110.             super.drawBackground(drawContext);
  111.         }
  112.     }
  113. }
C05E15_LargeTable.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. /*
  10.  * This example was written by Bruno Lowagie
  11.  * in the context of the book: iText 7 building blocks
  12.  */
  13. package com.itextpdf.highlevel.chapter05;
  14.  
  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.Table;
  19. import com.itextpdf.test.annotations.WrapToTest;
  20. import java.io.File;
  21. import java.io.IOException;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class C05E15_LargeTable {
  28.    
  29.     public static final String DEST = "results/chapter05/large_table.pdf";
  30.        
  31.     public static void main(String args[]) throws IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new C05E15_LargeTable().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException {
  38.         //Initialize PDF document
  39.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  40.        
  41.         // Initialize document
  42.         Document document = new Document(pdf);
  43.         Table table = new Table(new float[]{100, 100, 100}, true);
  44.         table.addHeaderCell("Table header 1");
  45.         table.addHeaderCell("Table header 2");
  46.         table.addHeaderCell("Table header 3");
  47.         table.addFooterCell("Table footer 1");
  48.         table.addFooterCell("Table footer 2");
  49.         table.addFooterCell("Table footer 3");
  50.         document.add(table);
  51.         for (int i = 0; i < 1000; i++) {
  52.             table.addCell(String.format("Row %s; column 1", i + 1));
  53.             table.addCell(String.format("Row %s; column 2", i + 1));
  54.             table.addCell(String.format("Row %s; column 3", i + 1));
  55.             if (i %50 == 0) {
  56.                 table.flush();
  57.             }
  58.         }
  59.         table.complete();
  60.         document.close();
  61.     }
  62. }
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