iText pdf library
Website search

Colspan and rowspan

Some table examples involving rowspan and/or colspan.

ColspanRowspan.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-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/23989852/itext-combining-rowspan-and-colspan-pdfptable
 */
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.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 ColspanRowspan {
    public static final String DEST = "./target/sandbox/tables/colspan_rowspan.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColspanRowspan().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth();
        Cell cell = new Cell().add(new Paragraph(" 1,1 "));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph(" 1,2 "));
        table.addCell(cell);
        Cell cell23 = new Cell(2, 2).add(new Paragraph("multi 1,3 and 1,4"));
        table.addCell(cell23);
        cell = new Cell().add(new Paragraph(" 2,1 "));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph(" 2,2 "));
        table.addCell(cell);
        doc.add(table);
 
        doc.close();
    }
}
RowspanAbsolutePosition.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
/*
    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/23730886/row-span-not-working-for-writeselectedrows-method
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.io.image.ImageDataFactory;
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.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
 
import java.io.File;
 
public class RowspanAbsolutePosition {
    public static final String DEST = "./target/sandbox/tables/rowspan_absolute_position.pdf";
    public static final String IMG = "./src/test/resources/img/berlin2013.jpg";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RowspanAbsolutePosition().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table1 = new Table(new float[]{150, 200, 200});
        Cell cell = new Cell(1, 2).add(new Paragraph("{Month}"));
        cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
        Image img = new Image(ImageDataFactory.create(IMG));
        img.setWidth(UnitValue.createPercentValue(100));
        img.setAutoScale(true);
        Cell cell2 = new Cell(2, 1).add(img);
        Cell cell3 = new Cell(1, 2).add(new Paragraph("Mr Fname Lname"));
        cell3.setHorizontalAlignment(HorizontalAlignment.LEFT);
        table1.addCell(cell);
        table1.addCell(cell2);
        table1.addCell(cell3);
        doc.add(table1);
 
        doc.close();
    }
}
SimpleRowColspan.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
/*
    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/20016630/how-to-create-a-table-in-a-generated-pdf-using-itextsharp
 */
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.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 SimpleRowColspan {
    public static final String DEST = "./target/sandbox/tables/simple_row_colspan.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleRowColspan().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{1, 2, 2, 2, 1}));
 
        Cell cell;
        cell = new Cell(2, 1).add(new Paragraph("S/N"));
        table.addCell(cell);
        cell = new Cell(1, 3).add(new Paragraph("Name"));
        table.addCell(cell);
        cell = new Cell(2, 1).add(new Paragraph("Age"));
        table.addCell(cell);
        table.addCell("SURNAME");
        table.addCell("FIRST NAME");
        table.addCell("MIDDLE NAME");
        table.addCell("1");
        table.addCell("James");
        table.addCell("Fish");
        table.addCell("Stone");
        table.addCell("17");
        doc.add(table);
 
        doc.close();
    }
}
SimpleTable11.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-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/31146249/how-to-set-up-a-table-display-in-itextpdf
 */
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.SolidBorder;
import com.itextpdf.layout.element.Cell;
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 java.io.File;
 
public class SimpleTable11 {
    public static final String DEST = "./target/sandbox/tables/simple_table11.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable11().manipulatePdf(DEST);
    }
 
    public Cell createCell(String content, float borderWidth, int colspan, TextAlignment alignment) {
        Cell cell = new Cell(1, colspan).add(new Paragraph(content));
        cell.setTextAlignment(alignment);
        cell.setBorder(new SolidBorder(borderWidth));
        return cell;
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(UnitValue.createPercentArray(new float[]{1, 2, 1, 1, 1}));
        table.addCell(createCell("SKU", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Description", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Unit Price", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Quantity", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Extension", 2, 1, TextAlignment.LEFT));
        String[][] data = {
                {"ABC123", "The descriptive text may be more than one line and the text should wrap automatically",
                        "$5.00", "10", "$50.00"},
                {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
                {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
        };
        for (String[] row : data) {
            table.addCell(createCell(row[0], 1, 1, TextAlignment.LEFT));
            table.addCell(createCell(row[1], 1, 1, TextAlignment.LEFT));
            table.addCell(createCell(row[2], 1, 1, TextAlignment.RIGHT));
            table.addCell(createCell(row[3], 1, 1, TextAlignment.RIGHT));
            table.addCell(createCell(row[4], 1, 1, TextAlignment.RIGHT));
        }
        table.addCell(createCell("Totals", 2, 4, TextAlignment.LEFT));
        table.addCell(createCell("$1,552.00", 2, 1, TextAlignment.RIGHT));
        doc.add(table);
 
        doc.close();
    }
}
SimpleTable12.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
/*
    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/31263533/how-to-create-nested-column-using-itextsharp
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
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.layout.Document;
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.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
 
import java.io.File;
 
public class SimpleTable12 {
    public static final String DEST = "./target/sandbox/tables/simple_table12.pdf";
 
    protected PdfFont font;
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable12().manipulatePdf(DEST);
    }
 
    public Cell createCell(String content, int colspan, int rowspan, int border) {
        Cell cell = new Cell(rowspan, colspan).add(new Paragraph(content).setFont(font).setFontSize(10));
        cell.setBorder(null);
        if (8 == (border & 8)) {
            cell.setBorderRight(new SolidBorder(1));
            cell.setBorderBottom(new SolidBorder(1));
        }
        if (4 == (border & 4)) {
            cell.setBorderLeft(new SolidBorder(1));
        }
        if (2 == (border & 2)) {
            cell.setBorderBottom(new SolidBorder(1));
        }
        if (1 == (border & 1)) {
            cell.setBorderTop(new SolidBorder(1));
        }
        cell.setTextAlignment(TextAlignment.CENTER);
        return cell;
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        // Note that it is not necessary to create new PageSize object,
        // but for testing reasons (connected to parallelization) we call constructor here
        Document doc = new Document(pdfDoc, PageSize.A4.rotate());
 
        font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        Table table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();
        table.addCell(createCell("Examination", 1, 2, 15));
        table.addCell(createCell("Board", 1, 2, 15));
        table.addCell(createCell("Month and Year of Passing", 1, 2, 15));
        table.addCell(createCell("", 1, 1, 1));
        table.addCell(createCell("Marks", 2, 1, 1));
        table.addCell(createCell("Percentage", 1, 2, 15));
        table.addCell(createCell("Class / Grade", 1, 2, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("Obtained", 1, 1, 15));
        table.addCell(createCell("Out of", 1, 1, 15));
        table.addCell(createCell("12th / I.B. Diploma", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("Aggregate (all subjects)", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        table.addCell(createCell("", 1, 1, 15));
        doc.add(table);
 
        doc.close();
    }
}
SimpleTable2.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
/*
    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 and Nishanthi Grashia in answer to the following question:
 * http://stackoverflow.com/questions/24359321/adding-row-to-a-table-in-pdf-using-itext
 */
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.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 SimpleTable2 {
    public static final String DEST = "./target/sandbox/tables/simple_table2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable2().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();
        Cell cell = new Cell(2, 1).add(new Paragraph("hi"));
        table.addCell(cell);
        for (int i = 0; i < 14; i++) {
            table.addCell("hi");
        }
        doc.add(table);
 
        doc.close();
    }
}
SimpleTable9.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
/*
    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/30032558/how-to-show-a-cellcolumn-with-its-row-values-of-a-pdftableitextsharp-in-next
 */
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.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 SimpleTable9 {
    public static final String DEST = "./target/sandbox/tables/simple_table9.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable9().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        doc.add(new Paragraph("With 3 columns:"));
        Table table = new Table(UnitValue.createPercentArray(new float[]{10, 10, 80}));
        table.setMarginTop(5);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Col c");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.");
        doc.add(table);
        doc.add(new Paragraph("With 2 columns:"));
        table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.setMarginTop(5);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));
        table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.")));
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));
        table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.")));
        doc.add(table);
 
        doc.close();
    }
}
TableMeasurements.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
/*
    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/34539028
 */
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.Cell;
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 java.io.File;
 
public class TableMeasurements {
    public static final String DEST = "./target/sandbox/tables/tables_measurements.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableMeasurements().manipulatePdf(DEST);
    }
 
    public static float millimetersToPoints(float value) {
        return (value / 25.4f) * 72f;
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(UnitValue.createPercentArray(10)).useAllAvailableWidth();
        table.setWidth(millimetersToPoints(100));
        table.addCell(getCell(10));
        table.addCell(getCell(5));
        table.addCell(getCell(3));
        table.addCell(getCell(2));
        table.addCell(getCell(3));
        table.addCell(getCell(5));
        table.addCell(getCell(1));
        doc.add(table);
        doc.close();
    }
 
    private Cell getCell(int cm) {
        Cell cell = new Cell(1, cm);
        Paragraph p = new Paragraph(
                String.format("%smm", 10 * cm)).setFontSize(8);
        p.setTextAlignment(TextAlignment.CENTER);
        p.setMultipliedLeading(0.5f);
        p.setMarginTop(0);
        cell.add(p);
        return cell;
    }
}
ColspanRowspan.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
/*
    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 ColspanRowspan
    {
        public static readonly string DEST = "../../results/sandbox/tables/colspan_rowspan.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new ColspanRowspan().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table = new Table(UnitValue.CreatePercentArray(4)).UseAllAvailableWidth();
            Cell cell = new Cell().Add(new Paragraph(" 1,1 "));
            table.AddCell(cell);
 
            cell = new Cell().Add(new Paragraph(" 1,2 "));
            table.AddCell(cell);
 
            Cell cell23 = new Cell(2, 2).Add(new Paragraph("multi 1,3 and 1,4"));
            table.AddCell(cell23);
 
            cell = new Cell().Add(new Paragraph(" 2,1 "));
            table.AddCell(cell);
 
            cell = new Cell().Add(new Paragraph(" 2,2 "));
            table.AddCell(cell);
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
RowspanAbsolutePosition.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
/*
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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Samples.Sandbox.Tables
{
    public class RowspanAbsolutePosition
    {
        public static readonly string DEST = "../../results/sandbox/tables/rowspan_absolute_position.pdf";
 
        public static readonly String IMG = "../../resources/img/berlin2013.jpg";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new RowspanAbsolutePosition().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table1 = new Table(new float[] {150, 200, 200});
 
            Cell cell = new Cell(1, 2).Add(new Paragraph("{Month}"));
            cell.SetHorizontalAlignment(HorizontalAlignment.LEFT);
 
            Image img = new Image(ImageDataFactory.Create(IMG));
            img.SetWidth(UnitValue.CreatePercentValue(100));
            img.SetAutoScale(true);
 
            Cell cell2 = new Cell(2, 1).Add(img);
            Cell cell3 = new Cell(1, 2).Add(new Paragraph("Mr Fname Lname"));
            cell3.SetHorizontalAlignment(HorizontalAlignment.LEFT);
 
            table1.AddCell(cell);
            table1.AddCell(cell2);
            table1.AddCell(cell3);
 
            doc.Add(table1);
 
            doc.Close();
        }
    }
}
SimpleRowColspan.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
/*
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 SimpleRowColspan
    {
        public static readonly string DEST = "../../results/sandbox/tables/simple_row_colspan.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SimpleRowColspan().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table = new Table(UnitValue.CreatePercentArray(new float[] {1, 2, 2, 2, 1}));
 
            Cell cell = new Cell(2, 1).Add(new Paragraph("S/N"));
            table.AddCell(cell);
 
            cell = new Cell(1, 3).Add(new Paragraph("Name"));
            table.AddCell(cell);
 
            cell = new Cell(2, 1).Add(new Paragraph("Age"));
            table.AddCell(cell);
 
            table.AddCell("SURNAME");
            table.AddCell("FIRST NAME");
            table.AddCell("MIDDLE NAME");
            table.AddCell("1");
            table.AddCell("James");
            table.AddCell("Fish");
            table.AddCell("Stone");
            table.AddCell("17");
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
SimpleTable11.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
/*
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 SimpleTable11
    {
        public static readonly string DEST = "../../results/sandbox/tables/simple_table11.pdf";
 
        private String [][] data =
        {
            new[]
            {
                "ABC123", "The descriptive text may be more than one line and the text should wrap automatically",
                "$5.00", "10", "$50.00"
            },
            new[] {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
            new[] {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
        };
        
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SimpleTable11().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table = new Table(UnitValue.CreatePercentArray(new float[] {1, 2, 1, 1, 1}));
           
            table.AddCell(CreateCell("SKU", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Description", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Unit Price", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Quantity", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Extension", 2, 1, TextAlignment.LEFT));
            
            foreach (String[] row in data)
            {
                table.AddCell(CreateCell(row[0], 1, 1, TextAlignment.LEFT));
                table.AddCell(CreateCell(row[1], 1, 1, TextAlignment.LEFT));
                table.AddCell(CreateCell(row[2], 1, 1, TextAlignment.RIGHT));
                table.AddCell(CreateCell(row[3], 1, 1, TextAlignment.RIGHT));
                table.AddCell(CreateCell(row[4], 1, 1, TextAlignment.RIGHT));
            }
 
            table.AddCell(CreateCell("Totals", 2, 4, TextAlignment.LEFT));
            table.AddCell(CreateCell("$1,552.00", 2, 1, TextAlignment.RIGHT));
 
            doc.Add(table);
 
            doc.Close();
        }
 
        private Cell CreateCell(String content, float borderWidth, int colspan, TextAlignment? alignment)
        {
            Cell cell = new Cell(1, colspan).Add(new Paragraph(content));
            cell.SetTextAlignment(alignment);
            cell.SetBorder(new SolidBorder(borderWidth));
            return cell;
        }
    }
}
SimpleTable12.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
/*
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.Font;
using iText.Kernel.Geom;
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 SimpleTable12
    {
        public static readonly string DEST = "../../results/sandbox/tables/simple_table12.pdf";
 
        private PdfFont font;
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SimpleTable12().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, PageSize.A4.Rotate());
 
            font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
 
            Table table = new Table(UnitValue.CreatePercentArray(8)).UseAllAvailableWidth();
            
            table.AddCell(CreateCell("Examination", 1, 2, 15));
            table.AddCell(CreateCell("Board", 1, 2, 15));
            table.AddCell(CreateCell("Month and Year of Passing", 1, 2, 15));
            table.AddCell(CreateCell("", 1, 1, 1));
            table.AddCell(CreateCell("Marks", 2, 1, 1));
            table.AddCell(CreateCell("Percentage", 1, 2, 15));
            table.AddCell(CreateCell("Class / Grade", 1, 2, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("Obtained", 1, 1, 15));
            table.AddCell(CreateCell("Out of", 1, 1, 15));
            table.AddCell(CreateCell("12th / I.B. Diploma", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("Aggregate (all subjects)", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
            table.AddCell(CreateCell("", 1, 1, 15));
 
            doc.Add(table);
 
            doc.Close();
        }
 
        private Cell CreateCell(String content, int colspan, int rowspan, int border)
        {
            Cell cell = new Cell(rowspan, colspan).Add(new Paragraph(content).SetFont(font).SetFontSize(10));
            cell
                .SetBorder(Border.NO_BORDER)
                .SetTextAlignment(TextAlignment.CENTER);
 
            if (8 == (border & 8))
            {
                cell.SetBorderRight(new SolidBorder(1));
                cell.SetBorderBottom(new SolidBorder(1));
            }
 
            if (4 == (border & 4))
            {
                cell.SetBorderLeft(new SolidBorder(1));
            }
 
            if (2 == (border & 2))
            {
                cell.SetBorderBottom(new SolidBorder(1));
            }
 
            if (1 == (border & 1))
            {
                cell.SetBorderTop(new SolidBorder(1));
            }
            
            return cell;
        }
    }
}
SimpleTable2.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 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 SimpleTable2
    {
        public static readonly string DEST = "../../results/sandbox/tables/simple_table2.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SimpleTable2().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table = new Table(UnitValue.CreatePercentArray(8)).UseAllAvailableWidth();
            
            Cell cell = new Cell(2, 1).Add(new Paragraph("hi"));
            table.AddCell(cell);
 
            for (int i = 0; i < 14; i++)
            {
                table.AddCell("hi");
            }
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
SimpleTable9.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
/*
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 SimpleTable9
    {
        public static readonly string DEST = "../../results/sandbox/tables/simple_table9.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new SimpleTable9().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            doc.Add(new Paragraph("With 3 columns:"));
 
            Table table = new Table(UnitValue.CreatePercentArray(new float[] {10, 10, 80}));
            table.SetMarginTop(5);
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Col c");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell("This is a long description for column c. "
                          + "It needs much more space hence we made sure that the third column is wider.");
            doc.Add(table);
 
            doc.Add(new Paragraph("With 2 columns:"));
 
            table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.SetMarginTop(5);
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell(new Cell(1, 2).Add(new Paragraph("Value b")));
            table.AddCell(new Cell(1, 2).Add(new Paragraph("This is a long description for column c. "
                                                           + "It needs much more space hence we made sure that the third column is wider.")));
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell(new Cell(1, 2).Add(new Paragraph("Value b")));
            table.AddCell(new Cell(1, 2).Add(new Paragraph("This is a long description for column c. "
                                                           + "It needs much more space hence we made sure that the third column is wider.")));
 
            doc.Add(table);
 
            doc.Close();
        }
    }
}
TableMeasurements.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
/*
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 TableMeasurements
    {
        public static readonly string DEST = "../../results/sandbox/tables/tables_measurements.pdf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new TableMeasurements().ManipulatePdf(DEST);
        }
 
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            Table table = new Table(UnitValue.CreatePercentArray(10)).UseAllAvailableWidth();
            table.SetWidth(MillimetersToPoints(100));
            table.AddCell(GetCell(10));
            table.AddCell(GetCell(5));
            table.AddCell(GetCell(3));
            table.AddCell(GetCell(2));
            table.AddCell(GetCell(3));
            table.AddCell(GetCell(5));
            table.AddCell(GetCell(1));
 
            doc.Add(table);
 
            doc.Close();
        }
 
        private static float MillimetersToPoints(float value)
        {
            return (value / 25.4f) * 72f;
        }
 
        private Cell GetCell(int cm)
        {
            Cell cell = new Cell(1, cm);
            Paragraph p = new Paragraph(String.Format("{0}mm", 10 * cm)).SetFontSize(8);
            p.SetTextAlignment(TextAlignment.CENTER);
            p.SetMultipliedLeading(0.5f);
            p.SetMarginTop(0);
            cell.Add(p);
            return cell;
        }
    }
}

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