Splitting tables

31st May 2016
admin-marketing

Examples that show what happens when a table doesn't fit the page:

SplitRowAtEndOfPage.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
 */
package com.itextpdf.samples.sandbox.tables;
 
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;
 
public class SplitRowAtEndOfPage {
    public static final String DEST = "./target/sandbox/tables/split_row_at_end_of_page.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SplitRowAtEndOfPage().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        // Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
        table.setWidth(550);
        // the number of iterations has been changed in order to provide the same as in itext5 example
        for (int i = 0; i < 6; i++) {
            Cell cell;
            if (i == 5) {
                cell = new Cell().add(new Paragraph("Three\nLines\nHere"));
            } else {
                cell = new Cell().add(new Paragraph(Integer.toString(i)));
            }
            table.addCell(cell);
        }
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(612, 237));
        doc.add(table);
        doc.close();
    }
}
SplitRowAtSpecificRow.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24665167/table-keeprowstogether-in-itext-5-5-1-doesnt-seem-to-work-correctly
 */
package com.itextpdf.samples.sandbox.tables;
 
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;
 
public class SplitRowAtSpecificRow {
    public static final String DEST = "./target/sandbox/tables/split_row_at_specific_row.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SplitRowAtSpecificRow().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        // Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
        table.setWidth(550);
        // the number of iterations has been changed in order to provide the same as in itext5 example
        for (int i = 0; i < 6; i++) {
            Cell cell;
            if (i == 5) {
                cell = new Cell().add(new Paragraph("Three\nLines\nHere"));
            } else {
                cell = new Cell().add(new Paragraph(Integer.toString(i)));
            }
            table.addCell(cell);
        }
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(612, 237));
        doc.add(table);
        doc.close();
    }
}
Splitting.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
 */
package com.itextpdf.samples.sandbox.tables;
 
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.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
 
import java.io.File;
 
public class Splitting {
    public static final String DEST = "./target/sandbox/tables/splitting.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Splitting().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Paragraph p = new Paragraph("Test");
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        for (int i = 0; i < 27; i++) {
            doc.add(p);
        }
        doc.add(table);
        for (int i = 0; i < 23; i++) {
            doc.add(p);
        }
        table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
 
        Table nesting = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        Cell cell = new Cell().add(table);
        cell.setBorder(Border.NO_BORDER);
        nesting.addCell(cell.setKeepTogether(true));
        doc.add(nesting);
        doc.close();
    }
}
Splitting2.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-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.UnitValue;
 
import java.io.File;
 
import org.junit.Ignore;
 
public class Splitting2 {
    public static final String DEST = "./target/sandbox/tables/splitting2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Splitting2().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Paragraph p = new Paragraph("Test");
        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        for (int i = 0; i < 27; i++) {
            doc.add(p);
        }
        doc.add(table);
        for (int i = 0; i < 23; i++) {
            doc.add(p);
        }
        table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        table.setKeepTogether(true);
        doc.add(table);
        doc.close();
    }
}
TableSplitTest.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Ramesh in the context of a question on SO:
 * http://stackoverflow.com/questions/29345454/itext-avoid-row-splitting-in-table
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.LineSeparator;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.property.VerticalAlignment;
 
import java.io.File;
 
public class TableSplitTest {
    public static final String DEST = "./target/sandbox/tables/tables_split_test.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableSplitTest().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(595, 842));
        doc.setMargins(55, 15, 35, 15);
 
        ILineDrawer line = new SolidLine(2);
        line.setColor(ColorConstants.LIGHT_GRAY);
        LineSeparator tableEndSeparator = new LineSeparator(line);
 
        tableEndSeparator.setMarginTop(10);
 
        String[] header = new String[]{"Header1", "Header2", "Header3",
                "Header4", "Header5"};
        String[] content = new String[]{"column 1", "column 2",
                "some Text in column 3", "Test data ", "column 5"};
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 4, 3, 2}));
        table.setWidth(UnitValue.createPercentValue(98));
 
        for (String columnHeader : header) {
            Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
                    PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD))
                    .setFontSize(10));
            headerCell.setTextAlignment(TextAlignment.CENTER);
            headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            headerCell.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1));
            headerCell.setPadding(8);
            table.addHeaderCell(headerCell);
        }
        for (int i = 0; i < 15; i++) {
            int j = 0;
            for (String text : content) {
                if (i == 13 && j == 3) {
                    text = "Test data \n Test data \n Test data";
                }
                j++;
                Cell cell = new Cell().add(new Paragraph(text).setFont(
                        PdfFontFactory.createFont(StandardFonts.HELVETICA))
                        .setFontSize(10));
                cell.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1));
                cell.setPaddingLeft(5);
                cell.setPaddingTop(5);
                cell.setPaddingRight(5);
                cell.setPaddingBottom(5);
                table.addCell(cell);
            }
        }
        doc.add(table);
        doc.add(tableEndSeparator);
        for (int k = 0; k < 5; k++) {
            Paragraph info = new Paragraph("Some title").setFont(
                    PdfFontFactory.createFont(StandardFonts.HELVETICA))
                    .setFontSize(10);
            info.setMarginTop(12f);
            doc.add(info);
            table = new Table(UnitValue.createPercentArray(new float[]{3, 2, 4, 3, 2}));
            table.setWidth(UnitValue.createPercentValue(98));
            table.setMarginTop(15);
 
            for (String columnHeader : header) {
                Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
                        PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD))
                        .setFontSize(10));
                headerCell.setTextAlignment(TextAlignment.CENTER);
                headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
                headerCell.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1));
                headerCell.setPaddingLeft(8);
                headerCell.setPaddingTop(8);
                headerCell.setPaddingRight(8);
                headerCell.setPaddingBottom(8);
                table.addHeaderCell(headerCell);
            }
            for (String text : content) {
                Cell cell = new Cell().add(new Paragraph(text).setFont(
                        PdfFontFactory.createFont(StandardFonts.HELVETICA))
                        .setFontSize(10));
                cell.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1));
                cell.setPaddingLeft(5);
                cell.setPaddingTop(5);
                cell.setPaddingRight(5);
                cell.setPaddingBottom(5);
                table.addCell(cell);
            }
            doc.add(table);
            doc.add(tableEndSeparator);
        }
 
        doc.close();
    }
}
SplitRowAtEndOfPage.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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Samples.Sandbox.Tables
{
    public class SplitRowAtEndOfPage
    {
        public static readonly string DEST = "../../results/sandbox/tables/split_row_at_end_of_page.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SplitRowAtEndOfPage().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
 
            // Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
            table.SetWidth(550);
 
            for (int i = 0; i < 6; i++)
            {
                Cell cell = new Cell()
                    .Add(new Paragraph((i == 5) ? "Three\nLines\nHere" : i.ToString()));
                
                table.AddCell(cell);
            }
 
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(612, 237));
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
SplitRowAtSpecificRow.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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.IO.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Samples.Sandbox.Tables
{
    public class SplitRowAtSpecificRow
    {
        public static readonly string DEST = "../../results/sandbox/tables/split_row_at_specific_row.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SplitRowAtSpecificRow().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
 
            // Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
            table.SetWidth(550);
 
            for (int i = 0; i < 6; i++)
            {
                Cell cell = new Cell()
                    .Add(new Paragraph((i == 5) ? "Three\nLines\nHere" : i.ToString()));
 
                table.AddCell(cell);
            }
 
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(612, 237));
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
Splitting.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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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.Samples.Sandbox.Tables
{
    public class Splitting
    {
        public static readonly string DEST = "../../results/sandbox/tables/splitting.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new Splitting().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Paragraph p = new Paragraph("Test");
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
 
            for (int i = 1; i < 6; i++)
            {
                table.AddCell("key " + i);
                table.AddCell("value " + i);
            }
 
            for (int i = 0; i < 27; i++)
            {
                doc.Add(p);
            }
 
            doc.Add(table);
 
            for (int i = 0; i < 24; i++)
            {
                doc.Add(p);
            }
 
            table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
 
            for (int i = 1; i < 6; i++)
            {
                table.AddCell("key " + i);
                table.AddCell("value " + i);
            }
 
            Table nesting = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
 
            Cell cell = new Cell().Add(table);
            cell.SetBorder(Border.NO_BORDER);
            nesting.AddCell(cell.SetKeepTogether(true));
 
            doc.Add(nesting);
 
            doc.Close();
        }
    }
}
Splitting2.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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Samples.Sandbox.Tables
{
    public class Splitting2
    {
        public static readonly string DEST = "../../results/sandbox/tables/splitting2.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new Splitting2().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Paragraph p = new Paragraph("Test");
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
 
            for (int i = 1; i < 6; i++)
            {
                table.AddCell("key " + i);
                table.AddCell("value " + i);
            }
 
            for (int i = 0; i < 27; i++)
            {
                doc.Add(p);
            }
 
            doc.Add(table);
 
            for (int i = 0; i < 24; i++)
            {
                doc.Add(p);
            }
 
            table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
 
            for (int i = 1; i < 6; i++)
            {
                table.AddCell("key " + i);
                table.AddCell("value " + i);
            }
 
            table.SetKeepTogether(true);
 
            doc.Add(table);
            
            doc.Close();
        }
    }
}
TableSplitTest.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Samples.Sandbox.Tables
{
    public class TableSplitTest
    {
        public static readonly string DEST = "../../results/sandbox/tables/tables_split_test.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new TableSplitTest().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(595, 842));
 
            doc.SetMargins(55, 15, 35, 15);
 
            ILineDrawer line = new SolidLine(2);
            line.SetColor(ColorConstants.LIGHT_GRAY);
 
            LineSeparator tableEndSeparator = new LineSeparator(line);
            tableEndSeparator.SetMarginTop(10);
 
            String[] header = {"Header1", "Header2", "Header3", "Header4", "Header5"};
            String[] content = {"column 1", "column 2", "some Text in column 3", "Test data ", "column 5"};
 
            Table table = new Table(
                    UnitValue.CreatePercentArray(new float[] {3, 2, 4, 3, 2})).UseAllAvailableWidth();
 
            foreach (String columnHeader in header)
            {
                Paragraph headerParagraph = new Paragraph(columnHeader)
                    .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD))
                    .SetFontSize(10);
 
                Cell headerCell = new Cell()
                    .Add(headerParagraph)
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetVerticalAlignment(VerticalAlignment.MIDDLE)
                    .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
                    .SetPadding(8);
 
                table.AddHeaderCell(headerCell);
            }
 
            for (int i = 0; i < 15; i++)
            {
                int j = 0;
 
                foreach (String text in content)
                {
                    Paragraph paragraph = new Paragraph((i == 13 && j == 3) ? "Test data \n Test data \n Test data" : text)
                        .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
                        .SetFontSize(10);
 
                    Cell cell = new Cell()
                        .Add(paragraph)
                        .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
                        .SetPaddingLeft(5)
                        .SetPaddingTop(5)
                        .SetPaddingRight(5)
                        .SetPaddingBottom(5);
 
                    table.AddCell(cell);
 
                    j++;
                }
            }
 
            doc.Add(table);
            doc.Add(tableEndSeparator);
 
            for (int k = 0; k < 5; k++)
            {
                Paragraph info = new Paragraph("Some title")
                    .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
                    .SetFontSize(10)
                    .SetMarginTop(12);
                
                doc.Add(info);
 
                table = new Table(
                    UnitValue.CreatePercentArray(new float[] {3, 2, 4, 3, 2})).UseAllAvailableWidth();
                table.SetMarginTop(15);
 
                foreach (String columnHeader in header)
                {
                    Paragraph paragraph = new Paragraph(columnHeader)
                        .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD))
                        .SetFontSize(10);
                    
                    Cell headerCell = new Cell()
                        .Add(paragraph)
                        .SetTextAlignment(TextAlignment.CENTER)
                        .SetVerticalAlignment(VerticalAlignment.MIDDLE)
                        .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
                        .SetPaddingLeft(8)
                        .SetPaddingTop(8)
                        .SetPaddingRight(8)
                        .SetPaddingBottom(8);
 
                    table.AddHeaderCell(headerCell);
                }
 
                foreach (String text in content)
                {
                    Paragraph paragraph = new Paragraph(text)
                        .SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
                        .SetFontSize(10);
                    
                    Cell cell = new Cell()
                        .Add(paragraph)
                        .SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
                        .SetPaddingLeft(5)
                        .SetPaddingTop(5)
                        .SetPaddingRight(5)
                        .SetPaddingBottom(5);
                    
                    table.AddCell(cell);
                }
 
                doc.Add(table);
                doc.Add(tableEndSeparator);
            }
 
            doc.Close();
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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