iText pdf library
Website search

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

C05E01_MyFirstTable.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E01_MyFirstTable {
    
    public static final String DEST = "results/chapter05/my_first_table.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E01_MyFirstTable().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(new float[] {1, 1, 1});
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E02_ColumnWidths1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E02_ColumnWidths1 {
 
    public static final String DEST = "results/chapter05/column_widths1.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E02_ColumnWidths1().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(new float[]{200, 100, 100});
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E02_ColumnWidths2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E02_ColumnWidths2 {
 
    public static final String DEST = "results/chapter05/column_widths2.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E02_ColumnWidths2().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{1, 1, 1}));
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E02_ColumnWidths3.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E02_ColumnWidths3 {
 
    public static final String DEST = "results/chapter05/column_widths3.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E02_ColumnWidths3().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setWidth(450);
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E02_ColumnWidths4.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E02_ColumnWidths4 {
    
    public static final String DEST = "results/chapter05/column_widths4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E02_ColumnWidths4().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E02_ColumnWidths5.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E02_ColumnWidths5 {
 
    public static final String DEST = "results/chapter05/column_widths5.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E02_ColumnWidths5().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(new float[]{2, 1, 1});
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
    }
}

C05E03_CellAlignment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E03_CellAlignment {
    
    public static final String DEST = "results/chapter05/cell_alignment.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E03_CellAlignment().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.setTextAlignment(TextAlignment.CENTER);
        table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2"))
            .setTextAlignment(TextAlignment.RIGHT));
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        Cell cell = new Cell()
            .add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT))
            .add(new Paragraph("Center"))
            .add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("Middle"))
            .setVerticalAlignment(VerticalAlignment.MIDDLE);
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("Bottom"))
            .setVerticalAlignment(VerticalAlignment.BOTTOM);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

C05E04_ColumnHeights.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.DashedBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E04_ColumnHeights {
    
    public static final String DEST = "results/chapter05/column_heights.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E04_ColumnHeights().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        Paragraph p =
            new Paragraph("The Strange Case of\nDr. Jekyll\nand\nMr. Hyde")
            .setBorder(new DashedBorder(0.3f));
        
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{1}));
        table.setWidth(UnitValue.createPercentValue(100));
        table.addCell(p);
        Cell cell = new Cell().setHeight(45).add(p);
        table.addCell(cell);
        cell = new Cell().setMinHeight(45).add(p);
        table.addCell(cell);
        cell = new Cell().setMinHeight(135).add(p);
        table.addCell(cell);
        cell = new Cell().setMaxHeight(45).add(p);
        table.addCell(cell);
        cell = new Cell().setMaxHeight(135).add(p);
        table.addCell(cell);
        cell = new Cell().add(p).setRotationAngle(Math.PI / 6);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

C05E05_CellPadding.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E05_CellPadding {
 
    public static final String DEST = "results/chapter05/cell_padding.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E05_CellPadding().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setBackgroundColor(ColorConstants.ORANGE);
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(
            new Cell(1, 3).add(new Paragraph("Cell with colspan 3"))
                .setPadding(10).setBackgroundColor(ColorConstants.GREEN));
        table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2"))
            .setPaddingLeft(30)
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.BLUE));
        table.addCell(new Cell().add(new Paragraph("row 1; cell 1"))
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        table.addCell(new Cell().add(new Paragraph("row 1; cell 2")));
        table.addCell(new Cell().add(new Paragraph("row 2; cell 1"))
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        table.addCell(new Cell().add(new Paragraph("row 2; cell 2")).setPadding(10)
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        document.add(table);
        document.close();
    }
}

C05E05_CellPaddingMargin.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.IRenderer;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E05_CellPaddingMargin {
 
    public static final String DEST = "results/chapter05/cell_margin_padding.pdf";
 
    private class MarginCellRenderer extends CellRenderer {
        public MarginCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new MarginCellRenderer((Cell)getModelElement());
        }
 
        @Override
        protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
            return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
        }
    }
 
    private class MarginCell extends Cell {
        public MarginCell() {
            super();
        }
 
        public MarginCell(int rowspan, int colspan) {
            super(rowspan, colspan);
        }
 
        @Override
        protected IRenderer makeNewRenderer() {
            return new MarginCellRenderer(this);
        }
    }
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E05_CellPaddingMargin().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setBackgroundColor(ColorConstants.ORANGE);
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(
            new MarginCell(1, 3).add(new Paragraph("Cell with colspan 3"))
                .setPadding(10).setMargin(5).setBackgroundColor(ColorConstants.GREEN));
        table.addCell(new MarginCell(2, 1).add(new Paragraph("Cell with rowspan 2"))
            .setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.BLUE));
        table.addCell(new MarginCell().add(new Paragraph("row 1; cell 1"))
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        table.addCell(new MarginCell().add(new Paragraph("row 1; cell 2")));
        table.addCell(new MarginCell().add(new Paragraph("row 2; cell 1")).setMargin(10)
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        table.addCell(new MarginCell().add(new Paragraph("row 2; cell 2")).setPadding(10)
            .setFontColor(ColorConstants.WHITE).setBackgroundColor(ColorConstants.RED));
        document.add(table);
        document.close();
    }
}

C05E06_CellBorders1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import java.io.File;
import java.io.IOException;
 
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.DashedBorder;
import com.itextpdf.layout.borders.DottedBorder;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment;
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E06_CellBorders1 {
 
    public static final String DEST = "results/chapter05/cell_borders1.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E06_CellBorders1().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER)
            .setTextAlignment(TextAlignment.CENTER);
        table.addCell(new Cell(1, 3)
            .add(new Paragraph("Cell with colspan 3"))
            .setVerticalAlignment(VerticalAlignment.MIDDLE)
            .setBorder(new DashedBorder(0.5f)));
        table.addCell(new Cell(2, 1)
            .add(new Paragraph("Cell with rowspan 2"))
            .setVerticalAlignment(VerticalAlignment.MIDDLE)
            .setBorderBottom(new DottedBorder(0.5f))
            .setBorderLeft(new DottedBorder(0.5f)));
        table.addCell(new Cell()
            .add(new Paragraph("row 1; cell 1"))
            .setBorder(new DottedBorder(ColorConstants.ORANGE, 0.5f)));
        table.addCell(new Cell()
            .add(new Paragraph("row 1; cell 2")));
        table.addCell(new Cell()
            .add(new Paragraph("row 2; cell 1"))
            .setBorderBottom(new SolidBorder(2)));
        table.addCell(new Cell()
            .add(new Paragraph("row 2; cell 2"))
            .setBorderBottom(new SolidBorder(2)));
        document.add(table);
 
        document.close();
    }
}

C05E06_CellBorders2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import java.io.File;
import java.io.IOException;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment;
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E06_CellBorders2 {
 
    public static final String DEST = "results/chapter05/cell_borders2.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E06_CellBorders2().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setBorder(new SolidBorder(3))
            .setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER)
            .setTextAlignment(TextAlignment.CENTER);
        table.addCell(new Cell(1, 3)
            .add(new Paragraph("Cell with colspan 3"))
            .setVerticalAlignment(VerticalAlignment.MIDDLE)
            .setBorder(Border.NO_BORDER));
        table.addCell(new Cell(2, 1)
            .add(new Paragraph("Cell with rowspan 2"))
            .setVerticalAlignment(VerticalAlignment.MIDDLE)
            .setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph("row 1; cell 1")));
        table.addCell(new Cell().add(new Paragraph("row 1; cell 2")));
        table.addCell(new Cell().add(new Paragraph("row 2; cell 1")));
        table.addCell(new Cell().add(new Paragraph("row 2; cell 2")));
        document.add(table);
 
        document.close();
    }
}

C05E06_CellBorders3.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import java.io.File;
import java.io.IOException;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E06_CellBorders3 {
 
    private class RoundedCornersCellRenderer extends CellRenderer {
        public RoundedCornersCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void drawBorder(DrawContext drawContext) {
            Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
            UnitValue[] margins = getMargins();
            Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.roundRectangle(rectangle.getX(), rectangle.getY(),
                rectangle.getWidth(), rectangle.getHeight(), 5).stroke();
            super.drawBorder(drawContext);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new RoundedCornersCellRenderer((Cell)getModelElement());
        }
 
        @Override
        protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
            return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
        }
    }
 
    private class RoundedCornersCell extends Cell {
        public RoundedCornersCell() {
            super();
            setBorder(Border.NO_BORDER);
            setMargin(2);
        }
 
        public RoundedCornersCell(int rowspan, int colspan) {
            super(rowspan, colspan);
            setBorder(Border.NO_BORDER);
            setVerticalAlignment(VerticalAlignment.MIDDLE);
            setMargin(5);
        }
 
        @Override
        protected IRenderer makeNewRenderer() {
            return new RoundedCornersCellRenderer(this);
        }
    }
 
    public static final String DEST = "results/chapter05/cell_borders3.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E06_CellBorders3().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
        table.setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER)
            .setTextAlignment(TextAlignment.CENTER);
        Cell cell = new RoundedCornersCell(1, 3)
            .add(new Paragraph("Cell with colspan 3"));
        table.addCell(cell);
        cell = new RoundedCornersCell(2, 1)
            .add(new Paragraph("Cell with rowspan 2"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 1; cell 1"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 1; cell 2"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 2; cell 1"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 2; cell 2"));
        table.addCell(cell);
        document.add(table);
 
        document.close();
    }
}

C05E06_CellBorders4.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import java.io.File;
import java.io.IOException;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.TableRenderer;
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E06_CellBorders4 {
 
    private class RoundedCornersTableRenderer extends TableRenderer {
 
        public RoundedCornersTableRenderer(Table modelElement) {
            super(modelElement);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new RoundedCornersTableRenderer((Table)getModelElement());
        }
 
        @Override
        protected void drawBorders(DrawContext drawContext) {
            Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
            UnitValue[] margins = getMargins();
            Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.roundRectangle(rectangle.getX() + 1, rectangle.getY() + 1,
                    rectangle.getWidth() - 2, rectangle.getHeight() -2, 5).stroke();
            super.drawBorder(drawContext);
        }
 
    }
 
    private class RoundedCornersCellRenderer extends CellRenderer {
        public RoundedCornersCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void drawBorder(DrawContext drawContext) {
            Rectangle occupiedAreaBBox = getOccupiedAreaBBox();
            UnitValue[] margins = getMargins();
            Rectangle rectangle = applyMargins(occupiedAreaBBox, margins, false);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.roundRectangle(rectangle.getX() + 1, rectangle.getY() + 1,
                    rectangle.getWidth() - 2, rectangle.getHeight() -2, 5).stroke();
            super.drawBorder(drawContext);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new RoundedCornersCellRenderer((Cell)getModelElement());
        }
 
        @Override
        protected Rectangle applyMargins(Rectangle rect, UnitValue[] margins, boolean reverse) {
            return rect.applyMargins(margins[0].getValue(), margins[1].getValue(), margins[2].getValue(), margins[3].getValue(), reverse);
        }
    }
 
    private class RoundedCornersCell extends Cell {
        public RoundedCornersCell() {
            super();
            setMargin(2);
        }
 
        public RoundedCornersCell(int rowspan, int colspan) {
            super(rowspan, colspan);
            setMargin(2);
        }
 
        @Override
        protected IRenderer makeNewRenderer() {
            return new RoundedCornersCellRenderer(this);
        }
    }
 
    public static final String DEST = "results/chapter05/cell_borders4.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E06_CellBorders4().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}))
            .setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER)
            .setTextAlignment(TextAlignment.CENTER);
        Cell cell = new RoundedCornersCell(1, 3)
            .add(new Paragraph("Cell with colspan 3"));
        table.addCell(cell);
        cell = new RoundedCornersCell(2, 1)
            .add(new Paragraph("Cell with rowspan 2"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 1; cell 1"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 1; cell 2"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 2; cell 1"));
        table.addCell(cell);
        cell = new RoundedCornersCell()
            .add(new Paragraph("row 2; cell 2"));
        table.addCell(cell);
        table.setNextRenderer(new RoundedCornersTableRenderer(table));
        document.add(table);
 
        document.close();
    }
}

C05E07_NestedTable.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E07_NestedTable {
 
    public static final String DEST = "results/chapter05/nested_tables.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E07_NestedTable().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(new float[]{1, 1})
            .setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
        table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
        Table inner = new Table(new float[]{1, 1});
        inner.addCell("row 1; cell 1");
        inner.addCell("row 1; cell 2");
        inner.addCell("row 2; cell 1");
        inner.addCell("row 2; cell 2");
        table.addCell(inner);
        document.add(table);
 
        table = new Table(new float[]{1, 1})
            .setMarginTop(10)
            .setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
        table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
        inner = new Table(new float[]{1, 1});
        inner.addCell("row 1; cell 1");
        inner.addCell("row 1; cell 2");
        inner.addCell("row 2; cell 1");
        inner.addCell("row 2; cell 2");
        table.addCell(new Cell().add(inner).setPadding(0));
        document.add(table);
 
        table = new Table(new float[]{1, 1})
            .setMarginTop(10)
            .setWidth(UnitValue.createPercentValue(80))
            .setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(new Cell(1, 2).add(new Paragraph("Cell with colspan 2")));
        table.addCell(new Cell().add(new Paragraph("Cell with rowspan 1")));
        inner = new Table(new float[]{1, 1})
            .setWidth(UnitValue.createPercentValue(100));
        inner.addCell("row 1; cell 1");
        inner.addCell("row 1; cell 2");
        inner.addCell("row 2; cell 1");
        inner.addCell("row 2; cell 2");
        table.addCell(new Cell().add(inner).setPadding(0));
        document.add(table);
 
        document.close();
    }
}

C05E08_JekyllHydeTableV1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E08_JekyllHydeTableV1 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table1.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E08_JekyllHydeTableV1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
        table.setWidth(UnitValue.createPercentValue(100));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        List header = resultSet.remove(0);
        for (String field : header) {
            table.addHeaderCell(field);
        }
        Cell cell = new Cell(1, 6).add(new Paragraph("Continued on next page..."));
        table.addFooterCell(cell)
            .setSkipLastFooter(true);
        for (List record : resultSet) {
            for (String field : record) {
                table.addCell(field);
            }
        }
        document.add(table);
        document.close();
    }
}

C05E09_JekyllHydeTableV2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E09_JekyllHydeTableV2 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table2.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E09_JekyllHydeTableV2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
        table.setWidth(UnitValue.createPercentValue(100));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        List header = resultSet.remove(0);
        for (String field : header) {
            table.addHeaderCell(field);
        }
        for (List record : resultSet) {
            for (String field : record) {
                table.addCell(field);
            }
        }
        Table outerTable = new Table(new float[]{1})
            .addHeaderCell("Continued from previous page:")
            .setSkipFirstHeader(true)
            .addCell(new Cell().add(table).setPadding(0));
        document.add(outerTable);
        document.close();
    }
}

C05E10_JekyllHydeTableV3.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E10_JekyllHydeTableV3 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table3.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E10_JekyllHydeTableV3().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        List header = resultSet.remove(0);
        for (String field : header) {
            table.addHeaderCell(field);
        }
        Cell cell;
        for (List record : resultSet) {
            cell = new Cell();
            File file = new File(String.format(
                "src/main/resources/img/%s.jpg", record.get(0)));
            if (file.exists()) {
                Image img = new Image(ImageDataFactory.create(file.getPath()));
                img.setAutoScaleWidth(true);
                img.setWidth(UnitValue.createPercentValue(100));
                cell.add(img);
            }
            else {
                cell.add(new Paragraph(record.get(0)));
            }
            table.addCell(cell);
            table.addCell(record.get(1));
            table.addCell(record.get(2));
            table.addCell(record.get(3));
            table.addCell(record.get(4));
            table.addCell(record.get(5));
        }
        document.add(table);
        document.close();
    }
}

C05E11_JekyllHydeTableV4.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E11_JekyllHydeTableV4 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E11_JekyllHydeTableV4().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 32}));
        table.setWidth(UnitValue.createPercentValue(100));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        table.addHeaderCell("imdb")
            .addHeaderCell("Information about the movie");
        Cell cell;
        for (List record : resultSet) {
            table.addCell(record.get(0));
            cell = new Cell()
                .add(new Paragraph(record.get(1)))
                .add(new Paragraph(record.get(2)))
                .add(new Paragraph(record.get(3)))
                .add(new Paragraph(record.get(4)))
                .add(new Paragraph(record.get(5)));
            table.addCell(cell);
        }
        document.add(table);
        document.close();
    }
}

C05E12_JekyllHydeTableV5.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E12_JekyllHydeTableV5 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table5.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E12_JekyllHydeTableV5().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 32}));
        table.setWidth(UnitValue.createPercentValue(100));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        table.addHeaderCell("imdb")
            .addHeaderCell("Information about the movie");
        Cell cell;
        for (List record : resultSet) {
            table.addCell(record.get(0));
            cell = new Cell()
                .add(new Paragraph(record.get(1)))
                .add(new Paragraph(record.get(2)))
                .add(new Paragraph(record.get(3)))
                .add(new Paragraph(record.get(4)))
                .add(new Paragraph(record.get(5)));
            cell.setKeepTogether(true);
            table.addCell(cell);
        }
        document.add(table);
        document.close();
    }
}

C05E13_JekyllHydeTableV6.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E13_JekyllHydeTableV6 {
 
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table6"
            + ".pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E13_JekyllHydeTableV6().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        List header = resultSet.remove(0);
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
        table.setWidth(UnitValue.createPercentValue(100));
        int nRows = resultSet.size();
        table.setNextRenderer(new AlternatingBackgroundTableRenderer(
            table, new Table.RowRange(0, nRows - 1)));
        for (String field : header) {
            table.addHeaderCell(field);
        }
        for (List record : resultSet) {
            for (String field : record) {
                table.addCell(field);
            }
        }
        document.add(table);
        document.close();
    }
}
 
class AlternatingBackgroundTableRenderer extends TableRenderer {
    private boolean isOdd = true;
 
    public AlternatingBackgroundTableRenderer(Table modelElement, Table.RowRange rowRange) {
        super(modelElement, rowRange);
    }
 
    public AlternatingBackgroundTableRenderer(Table modelElement) {
        super(modelElement);
    }
 
    @Override
    public AlternatingBackgroundTableRenderer getNextRenderer() {
        return new AlternatingBackgroundTableRenderer((Table) modelElement);
    }
 
    @Override
    public void draw(DrawContext drawContext) {
        for (int i = 0; i < rows.size() && null != rows.get(i) && null != rows.get(i)[0]; i++) {
            CellRenderer[] renderers = rows.get(i);
            Rectangle leftCell =
                renderers[0].getOccupiedAreaBBox();
            Rectangle rightCell =
                renderers[renderers.length - 1].getOccupiedAreaBBox();
            Rectangle rect = new Rectangle(
                leftCell.getLeft(), leftCell.getBottom(),
                rightCell.getRight() - leftCell.getLeft(),
                leftCell.getHeight());
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.saveState();
            if (isOdd) {
                canvas.setFillColor(ColorConstants.LIGHT_GRAY);
                isOdd = false;
            } else {
                canvas.setFillColor(ColorConstants.YELLOW);
                isOdd = true;
            }
            canvas.rectangle(rect);
            canvas.fill();
            canvas.restoreState();
        }
        super.draw(drawContext);
    }
}

C05E14_JekyllHydeTableV7.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C05E14_JekyllHydeTableV7 {
 
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter05/jekyll_hyde_table7.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E14_JekyllHydeTableV7().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        Document document = new Document(pdf, PageSize.A4.rotate());
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 14, 9, 4, 3}));
        table.setWidth(UnitValue.createPercentValue(100));
        List> resultSet = CsvTo2DList.convert(SRC, "|");
        List header = resultSet.remove(0);
        for (String field : header) {
            table.addHeaderCell(field);
        }
        for (List record : resultSet) {
            table.addCell(record.get(0));
            table.addCell(record.get(1));
            Cell cell = new Cell().add(new Paragraph(record.get(2)));
            cell.setNextRenderer(new RunlengthRenderer(cell, record.get(5)));
            table.addCell(cell);
            table.addCell(record.get(3));
            table.addCell(record.get(4));
            table.addCell(record.get(5));
        }
        document.add(table);
        document.close();
    }
 
    private class RunlengthRenderer extends CellRenderer {
        private int runlength;
 
        public RunlengthRenderer(Cell modelElement, String duration) {
            super(modelElement);
            if (duration.trim().isEmpty()) runlength = 0;
            else runlength = Integer.parseInt(duration);
        }
 
        @Override
        public IRenderer getNextRenderer() {
            return new RunlengthRenderer((Cell) getModelElement(), String.valueOf(runlength));
        }
 
        @Override
        public void drawBackground(DrawContext drawContext) {
            if (runlength == 0) return;
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.saveState();
            if (runlength < 90) {
                canvas.setFillColor(ColorConstants.GREEN);
            } else if (runlength > 240) {
                runlength = 240;
                canvas.setFillColor(ColorConstants.RED);
            } else {
                canvas.setFillColor(ColorConstants.ORANGE);
            }
            Rectangle rect = getOccupiedAreaBBox();
            canvas.rectangle(rect.getLeft(), rect.getBottom(),
                    rect.getWidth() * runlength / 240, rect.getHeight());
            canvas.fill();
            canvas.restoreState();
            super.drawBackground(drawContext);
        }
    }
}

C05E15_LargeTable.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter05;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C05E15_LargeTable {
    
    public static final String DEST = "results/chapter05/large_table.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C05E15_LargeTable().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        Table table = new Table(new float[]{100, 100, 100}, true);
        table.addHeaderCell("Table header 1");
        table.addHeaderCell("Table header 2");
        table.addHeaderCell("Table header 3");
        table.addFooterCell("Table footer 1");
        table.addFooterCell("Table footer 2");
        table.addFooterCell("Table footer 3");
        document.add(table);
        for (int i = 0; i < 1000; i++) {
            table.addCell(String.format("Row %s; column 1", i + 1));
            table.addCell(String.format("Row %s; column 2", i + 1));
            table.addCell(String.format("Row %s; column 3", i + 1));
            if (i %50 == 0) {
                table.flush();
            }
        }
        table.complete();
        document.close();
    }
}
C05E01_MyFirstTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C05E01_MyFirstTable {
        public const String DEST = "../../results/chapter05/my_first_table.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E01_MyFirstTable().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();
            table.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")));
            table.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")));
            table.AddCell("row 1; cell 1");
            table.AddCell("row 1; cell 2");
            table.AddCell("row 2; cell 1");
            table.AddCell("row 2; cell 2");
            document.Add(table);
            document.Close();
        }
    }
}
C05E02_ColumnWidths.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using NUnit.Framework.Constraints;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
 
    public class C05E02_ColumnWidths {
        public const String DEST = "../../results/chapter05/column_widths.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E02_ColumnWidths().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table.SetWidth(UnitValue.CreatePercentValue(80));
            table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            table.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")));
            table.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")));
            table.AddCell("row 1; cell 1");
            table.AddCell("row 1; cell 2");
            table.AddCell("row 2; cell 1");
            table.AddCell("row 2; cell 2");
            document.Add(table);
            document.Close();
        }
    }
}
C05E03_CellAlignment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C05E03_CellAlignment {
        public const String DEST = "../../results/chapter05/cell_alignment.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E03_CellAlignment().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table.SetWidth(UnitValue.CreatePercentValue(80));
            table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            table.SetTextAlignment(TextAlignment.CENTER);
            table.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")));
            table.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")).SetTextAlignment(TextAlignment.RIGHT));
            table.AddCell("row 1; cell 1");
            table.AddCell("row 1; cell 2");
            table.AddCell("row 2; cell 1");
            table.AddCell("row 2; cell 2");
            Cell cell = new Cell().Add(new Paragraph("Left").SetTextAlignment(TextAlignment.LEFT)).Add(new Paragraph("Center"
                )).Add(new Paragraph("Right").SetTextAlignment(TextAlignment.RIGHT));
            table.AddCell(cell);
            cell = new Cell().Add(new Paragraph("Middle")).SetVerticalAlignment(VerticalAlignment.MIDDLE);
            table.AddCell(cell);
            cell = new Cell().Add(new Paragraph("Bottom")).SetVerticalAlignment(VerticalAlignment.BOTTOM);
            table.AddCell(cell);
            document.Add(table);
            document.Close();
        }
    }
}
C05E04_ColumnHeights.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C05E04_ColumnHeights {
        public const String DEST = "../../results/chapter05/column_heights.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E04_ColumnHeights().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Paragraph p = new Paragraph("The Strange Case of\nDr. Jekyll\nand\nMr. Hyde").SetBorder(new DashedBorder(0.3f
                ));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
            table.AddCell(p);
            Cell cell = new Cell().SetMinHeight(16).Add(p);
            table.AddCell(cell);
            cell = new Cell().SetMinHeight(144).Add(p);
            table.AddCell(cell);
            cell = new Cell().Add(p).SetRotationAngle(Math.PI / 6);
            table.AddCell(cell);
            document.Add(table);
            document.Close();
        }
    }
}
C05E05_CellMarginPadding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C05E05_CellMarginPadding {
        public const String DEST = "../../results/chapter05/cell_margin_padding.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E05_CellMarginPadding().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table.SetBackgroundColor(ColorConstants.ORANGE);
            table.SetWidth(UnitValue.CreatePercentValue(80));
            table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            table.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")).SetPadding(10).SetMargin(5).SetBackgroundColor(ColorConstants
                .GREEN));
            table.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")).SetMarginTop(5).SetMarginBottom(5).SetPaddingLeft(
                30).SetFontColor(ColorConstants.WHITE).SetBackgroundColor(ColorConstants.BLUE));
            table.AddCell(new Cell().Add(new Paragraph("row 1; cell 1")).SetFontColor(ColorConstants.WHITE).SetBackgroundColor(ColorConstants.RED));
            table.AddCell(new Cell().Add(new Paragraph("row 1; cell 2")));
            table.AddCell(new Cell().Add(new Paragraph("row 2; cell 1")).SetMargin(10).SetFontColor(ColorConstants.WHITE).SetBackgroundColor(ColorConstants
                .RED));
            table.AddCell(new Cell().Add(new Paragraph("row 2; cell 2")).SetPadding(10).SetFontColor(ColorConstants.WHITE).SetBackgroundColor(
                ColorConstants.RED));
            document.Add(table);
            document.Close();
        }
    }
}
C05E06_CellBorders.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C05E06_CellBorders {
        private class RoundedCornersCellRenderer : CellRenderer {
            public RoundedCornersCellRenderer(Cell modelElement)
                : base(modelElement) {
            }
 
            public override void DrawBorder(DrawContext drawContext) {
                Rectangle occupiedAreaBBox = this.GetOccupiedAreaBBox();
                UnitValue[] margins = this.GetMargins();
                Rectangle rectangle = this.ApplyMargins(occupiedAreaBBox, margins, false);
                PdfCanvas canvas = drawContext.GetCanvas();
                canvas.RoundRectangle(rectangle.GetX() + 1, rectangle.GetY() + 1, rectangle.GetWidth() - 2, rectangle.GetHeight
                    () - 2, 5).Stroke();
                base.DrawBorder(drawContext);
            }
 
            protected override Rectangle ApplyMargins(Rectangle rect, UnitValue[] margins, bool reverse) {
                return rect.ApplyMargins(margins[0].GetValue(), margins[1].GetValue(), margins[2].GetValue(),
                    margins[3].GetValue(), reverse);
            }
        }
 
        private class RoundedCornersTableRenderer : TableRenderer
        {
            public RoundedCornersTableRenderer(Table modelElement)
                : base(modelElement) {}
 
            protected override void DrawBorders(DrawContext drawContext)
            {
                Rectangle occupiedAreaBBox = this.GetOccupiedAreaBBox();
                UnitValue[] margins = this.GetMargins();
                Rectangle rectangle = this.ApplyMargins(occupiedAreaBBox, margins, false);
                PdfCanvas canvas = drawContext.GetCanvas();
                canvas.RoundRectangle(rectangle.GetX() + 1, rectangle.GetY() + 1, rectangle.GetWidth() - 2, rectangle.GetHeight
                                                                                                                () - 2, 5).Stroke();
                base.DrawBorder(drawContext);
            }
        }
 
        private class RoundedCornersCell : Cell {
            public RoundedCornersCell()
                : base() {
                SetBorder(Border.NO_BORDER);
                SetMargin(2);
            }
 
            public RoundedCornersCell(int rowspan, int colspan)
                : base(rowspan, colspan) {
                SetBorder(Border.NO_BORDER);
                SetMargin(2);
                SetVerticalAlignment(VerticalAlignment.MIDDLE);
            }
 
            protected override IRenderer MakeNewRenderer() {
                return new C05E06_CellBorders.RoundedCornersCellRenderer(this);
            }
        }
 
        public const String DEST = "../../results/chapter05/cell_borders.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E06_CellBorders().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table1 = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table1.SetWidth(UnitValue.CreatePercentValue(80));
            table1.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            table1.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")).SetPadding(10).SetMargin(5).SetBorder(new DashedBorder
                (0.5f)));
            table1.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")).SetMarginTop(5).SetMarginBottom(5).SetBorderBottom
                (new DottedBorder(0.5f)).SetBorderLeft(new DottedBorder(0.5f)));
            table1.AddCell(new Cell().Add(new Paragraph("row 1; cell 1")).SetBorder(new DottedBorder(ColorConstants.ORANGE, 0.5f)));
            table1.AddCell(new Cell().Add(new Paragraph("row 1; cell 2")));
            table1.AddCell(new Cell().Add(new Paragraph("row 2; cell 1")).SetMargin(10).SetBorderBottom(new SolidBorder(2)));
            table1.AddCell(new Cell().Add(new Paragraph("row 2; cell 2")).SetPadding(10).SetBorderBottom(new SolidBorder(2)));
            document.Add(table1);
            Table table2 = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table2.SetMarginTop(10);
            table2.SetBorder(new SolidBorder(1));
            table2.SetWidth(UnitValue.CreatePercentValue(80));
            table2.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            table2.AddCell(new Cell(1, 3).Add(new Paragraph("Cell with colspan 3")).SetBorder(Border.NO_BORDER));
            table2.AddCell(new Cell(2, 1).Add(new Paragraph("Cell with rowspan 2")).SetBorder(Border.NO_BORDER));
            table2.AddCell(new Cell().Add(new Paragraph("row 1; cell 1")).SetBorder(Border.NO_BORDER));
            table2.AddCell(new Cell().Add(new Paragraph("row 1; cell 2")).SetBorder(Border.NO_BORDER));
            table2.AddCell(new Cell().Add(new Paragraph("row 2; cell 1")).SetBorder(Border.NO_BORDER));
            table2.AddCell(new Cell().Add(new Paragraph("row 2; cell 2")).SetBorder(Border.NO_BORDER));
            document.Add(table2);
            Table table3 = new Table(UnitValue.CreatePercentArray(new float[] { 2, 1, 1 }));
            table3.SetMarginTop(10);
            table3.SetWidth(UnitValue.CreatePercentValue(80));
            table3.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            Cell cell = new C05E06_CellBorders.RoundedCornersCell(1, 3).Add(new Paragraph("Cell with colspan 3"));
            table3.AddCell(cell);
            cell = new C05E06_CellBorders.RoundedCornersCell(2, 1).Add(new Paragraph("Cell with rowspan 2"));
            table3.AddCell(cell);
            cell = new C05E06_CellBorders.RoundedCornersCell().Add(new Paragraph("row 1; cell 1"));
            table3.AddCell(cell);
            cell = new C05E06_CellBorders.RoundedCornersCell().Add(new Paragraph("row 1; cell 2"));
            table3.AddCell(cell);
            cell = new C05E06_CellBorders.RoundedCornersCell().Add(new Paragraph("row 2; cell 1"));
            table3.AddCell(cell);
            cell = new C05E06_CellBorders.RoundedCornersCell().Add(new Paragraph("row 2; cell 2"));
            table3.AddCell(cell);
            table3.SetNextRenderer(new RoundedCornersTableRenderer(table3));
            document.Add(table3);
            document.Close();
        }
    }
}
C05E08_JekyllHydeTableV1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E08_JekyllHydeTableV1 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table1.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E08_JekyllHydeTableV1().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 2, 14, 9, 4, 3 })).UseAllAvailableWidth();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
 
            IList<String> header = resultSet[0];
            resultSet.RemoveAt(0);
            foreach (String field in header) {
                table.AddHeaderCell(field);
            }
            Cell cell = new Cell(1, 6).Add(new Paragraph("Continued on next page..."));
            table.AddFooterCell(cell).SetSkipLastFooter(true);
            foreach (IList<String> record in resultSet) {
                foreach (String field_1 in record) {
                    table.AddCell(field_1);
                }
            }
            document.Add(table);
            document.Close();
        }
    }
}
C05E09_JekyllHydeTableV2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E09_JekyllHydeTableV2 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table2.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E09_JekyllHydeTableV2().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 2, 14, 9, 4, 3 }))
                .UseAllAvailableWidth();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            IList<String> header = resultSet[0];
            resultSet.RemoveAt(0);
            foreach (String field in header) {
                table.AddHeaderCell(field);
            }
            foreach (IList<String> record in resultSet) {
                foreach (String field_1 in record) {
                    table.AddCell(field_1);
                }
            }
            Table outerTable = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth()
                .AddHeaderCell("Continued from previous page:").SetSkipFirstHeader(true)
                .AddCell(new Cell().Add(table).SetPadding(0));
            document.Add(outerTable);
            document.Close();
        }
    }
}
C05E10_JekyllHydeTableV3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E10_JekyllHydeTableV3 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table3.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E10_JekyllHydeTableV3().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 2, 14, 9, 4, 3 }));
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            IList<String> header = resultSet[0];
            resultSet.RemoveAt(0);
            foreach (String field in header) {
                table.AddHeaderCell(field);
            }
            Cell cell;
            foreach (IList<String> record in resultSet) {
                cell = new Cell();
                FileInfo file = new FileInfo(String.Format("../../resources/img/{0}.jpg", record[0]));
                if (file.Exists) {
                    iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(file.FullName));
                    img.SetAutoScaleWidth(true);
                    cell.Add(img);
                }
                else {
                    cell.Add(new Paragraph(record[0]));
                }
                table.AddCell(cell);
                table.AddCell(record[1]);
                table.AddCell(record[2]);
                table.AddCell(record[3]);
                table.AddCell(record[4]);
                table.AddCell(record[5]);
            }
            document.Add(table);
            document.Close();
        }
    }
}
C05E11_JekyllHydeTableV4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E11_JekyllHydeTableV4 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table4.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E11_JekyllHydeTableV4().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 32 }))
                .UseAllAvailableWidth();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            table.AddHeaderCell("imdb").AddHeaderCell("Information about the movie");
            Cell cell;
            foreach (IList<String> record in resultSet) {
                table.AddCell(record[0]);
                cell = new Cell().Add(new Paragraph(record[1])).Add(new Paragraph(record[2])).Add(new Paragraph(record[3])
                    ).Add(new Paragraph(record[4])).Add(new Paragraph(record[5]));
                table.AddCell(cell);
            }
            document.Add(table);
            document.Close();
        }
    }
}
C05E12_JekyllHydeTableV5.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E12_JekyllHydeTableV5 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table5.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E12_JekyllHydeTableV5().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 32 }))
                .UseAllAvailableWidth();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            table.AddHeaderCell("imdb").AddHeaderCell("Information about the movie");
            Cell cell;
            foreach (IList<String> record in resultSet) {
                table.AddCell(record[0]);
                cell = new Cell().Add(new Paragraph(record[1])).Add(new Paragraph(record[2])).Add(new Paragraph(record[3])
                    ).Add(new Paragraph(record[4])).Add(new Paragraph(record[5]));
                cell.SetKeepTogether(true);
                table.AddCell(cell);
            }
            document.Add(table);
            document.Close();
        }
    }
}
C05E13_JekyllHydeTableV6.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
 
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;
 
namespace iText.Highlevel.Chapter05
{
    /// <author>iText</author>
    public class C05E13_JekyllHydeTableV6
    {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table6" + ".pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E13_JekyllHydeTableV6().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            IList<String> header = resultSet[0];
            resultSet.RemoveAt(0);
            Table table = new Table(UnitValue.CreatePercentArray(new float[] {3, 2, 14, 9, 4, 3}));
            table.SetWidth(UnitValue.CreatePercentValue(100));
            int nRows = resultSet.Count;
            table.SetNextRenderer(new AlternatingBackgroundTableRenderer(table, new Table.RowRange(0, nRows - 1)));
            foreach (String field in header)
            {
                table.AddHeaderCell(field);
            }
 
            foreach (IList<String> record in resultSet)
            {
                foreach (String field_1 in record)
                {
                    table.AddCell(field_1);
                }
            }
 
            document.Add(table);
            document.Close();
        }
 
 
        private class AlternatingBackgroundTableRenderer : TableRenderer
        {
            private bool isOdd = true;
 
            public AlternatingBackgroundTableRenderer(Table modelElement, Table.RowRange rowRange)
                : base(modelElement, rowRange)
            {
            }
 
            public AlternatingBackgroundTableRenderer(Table modelElement)
                : base(modelElement)
            {
            }
 
            public override IRenderer GetNextRenderer()
            {
                return new AlternatingBackgroundTableRenderer((Table) modelElement);
            }
 
            public override void Draw(DrawContext drawContext)
            {
                for (int i = 0; i < rows.Count && null != rows[i] && null != rows[i][0]; i++)
                {
                    CellRenderer[] renderers = rows[i];
                    Rectangle leftCell = renderers[0].GetOccupiedAreaBBox();
                    Rectangle rightCell = renderers[renderers.Length - 1].GetOccupiedAreaBBox();
                    Rectangle rect = new Rectangle(leftCell.GetLeft(), leftCell.GetBottom(),
                        rightCell.GetRight() - leftCell.GetLeft
                            (), leftCell.GetHeight());
                    PdfCanvas canvas = drawContext.GetCanvas();
                    canvas.SaveState();
                    if (isOdd)
                    {
                        canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
                        isOdd = false;
                    }
                    else
                    {
                        canvas.SetFillColor(ColorConstants.YELLOW);
                        isOdd = true;
                    }
 
                    canvas.Rectangle(rect);
                    canvas.Fill();
                    canvas.RestoreState();
                }
 
                base.Draw(drawContext);
            }
        }
    }
}
C05E14_JekyllHydeTableV7.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>iText</author>
    public class C05E14_JekyllHydeTableV7 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter05/jekyll_hyde_table7.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E14_JekyllHydeTableV7().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf, PageSize.A4.Rotate());
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 2, 14, 9, 4, 3 }))
                .UseAllAvailableWidth();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            IList<String> header = resultSet[0];
            resultSet.RemoveAt(0);
            foreach (String field in header) {
                table.AddHeaderCell(field);
            }
            foreach (IList<String> record in resultSet) {
                table.AddCell(record[0]);
                table.AddCell(record[1]);
                Cell cell = new Cell().Add(new Paragraph(record[2]));
                cell.SetNextRenderer(new C05E14_JekyllHydeTableV7.RunlengthRenderer(cell, record[5]));
                table.AddCell(cell);
                table.AddCell(record[3]);
                table.AddCell(record[4]);
                table.AddCell(record[5]);
            }
            document.Add(table);
            document.Close();
        }
 
        private class RunlengthRenderer : CellRenderer {
            private int runlength;
 
            public RunlengthRenderer(Cell modelElement, String duration)
                : base(modelElement) {
                if (String.IsNullOrEmpty(duration.Trim())) {
                    this.runlength = 0;
                }
                else {
                    this.runlength = System.Convert.ToInt32(duration);
                }
            }
 
            public override IRenderer GetNextRenderer() {
                return new C05E14_JekyllHydeTableV7.RunlengthRenderer(((Cell)this.GetModelElement()), this.runlength
                    .ToString());
            }
 
            public override void DrawBackground(DrawContext drawContext) {
                if (this.runlength == 0) {
                    return;
                }
                PdfCanvas canvas = drawContext.GetCanvas();
                canvas.SaveState();
                if (this.runlength < 90) {
                    canvas.SetFillColor(ColorConstants.GREEN);
                }
                else {
                    if (this.runlength > 240) {
                        this.runlength = 240;
                        canvas.SetFillColor(ColorConstants.RED);
                    }
                    else {
                        canvas.SetFillColor(ColorConstants.ORANGE);
                    }
                }
                Rectangle rect = this.GetOccupiedAreaBBox();
                canvas.Rectangle(rect.GetLeft(), rect.GetBottom(), rect.GetWidth() * this.runlength / 240, rect.GetHeight(
                    ));
                canvas.Fill();
                canvas.RestoreState();
                base.DrawBackground(drawContext);
            }
        }
    }
}
C05E15_LargeTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter05 {
    /// <author>Bruno Lowagie (iText Software)</author>
 
    public class C05E15_LargeTable {
        public const String DEST = "../../results/chapter05/large_table.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C05E15_LargeTable().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            Table table = new Table(3, true);
            table.AddHeaderCell("Table header 1");
            table.AddHeaderCell("Table header 2");
            table.AddHeaderCell("Table header 3");
            table.AddFooterCell("Table footer 1");
            table.AddFooterCell("Table footer 2");
            table.AddFooterCell("Table footer 3");
            document.Add(table);
            for (int i = 0; i < 1000; i++) {
                table.AddCell(String.Format("Row {0}; column 1", i + 1));
                table.AddCell(String.Format("Row {0}; column 2", i + 1));
                table.AddCell(String.Format("Row {0}; column 3", i + 1));
                if (i % 50 == 0) {
                    table.Flush();
                }
            }
            table.Complete();
            document.Close();
        }
    }
}


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