iText pdf library
Website search

iText 7 Building Blocks - Chapter 4: AbstractElement examples (part 1)

C04E01_DivExample1.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
/*
    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.chapter04;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E01_DivExample1 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV1.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E01_DivExample1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List<String> record : resultSet) {
            Div div = new Div()
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            div.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            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.scaleToFit(10000, 120);
                div.add(img);
            }
            document.add(div);
        }
        document.close();
    }
    
}
C04E02_DivExample2.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
/*
    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.chapter04;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E02_DivExample2 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV2.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E02_DivExample2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List<String> record : resultSet) {
            Div div = new Div()
                .setKeepTogether(true)
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            div.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            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.scaleToFit(10000, 120);
                div.add(img);
            }
            document.add(div);
        }
        document.close();
    }
    
}
C04E03_LineSeparatorExample.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.chapter04;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E03_LineSeparatorExample {
 
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV3.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E03_LineSeparatorExample().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        SolidLine line = new SolidLine(1f);
        line.setColor(ColorConstants.RED);
        LineSeparator ls = new LineSeparator(line);
        ls.setWidth(UnitValue.createPercentValue(50));
        ls.setMarginTop(5);
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        for (List<String> record : resultSet) {
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            Div div = new Div()
                .setKeepTogether(true)
                .setBorderLeft(new SolidBorder(2))
                .setPaddingLeft(3)
                .setMarginBottom(10)
                .add(new Paragraph(movie.setFontSize(14f)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            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.scaleToFit(10000, 120);
                div.add(img);
            }
            div.add(ls);
            document.add(div);
        }
        document.close();
    }
 
}
C04E04_ParagraphAndDiv1.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
/*
    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.chapter04;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E04_ParagraphAndDiv1 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV1.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E04_ParagraphAndDiv1().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);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            Paragraph title = new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0);
            div = new Div()
                .add(title)
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);
 
        //Close document
        document.close();
    }
 
}
C04E05_ParagraphAndDiv2.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
/*
    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.chapter04;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E05_ParagraphAndDiv2 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV2.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E05_ParagraphAndDiv2().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);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0)
                .setKeepWithNext(true));
            div = new Div()
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);
 
        //Close document
        document.close();
    }
 
}
C04E05_ParagraphAndDiv3.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
/*
    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.chapter04;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.TextAlignment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E05_ParagraphAndDiv3 {
    public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST = "results/chapter04/jekyll_hydeV3.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E05_ParagraphAndDiv3().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);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
        document.setTextAlignment(TextAlignment.JUSTIFIED)
            .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
        
        BufferedReader br = new BufferedReader(new FileReader(SRC));
        String line;
        Div div = new Div();
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line)
                .setFont(bold).setFontSize(12)
                .setMarginBottom(0)
                .setKeepWithNext(true));
            div = new Div()
                .setFont(font).setFontSize(11)
                .setMarginBottom(18);
            while ((line = br.readLine()) != null) {
                div.add(
                    new Paragraph(line)
                        .setMarginBottom(0)
                        .setFirstLineIndent(36)
                        .setMultipliedLeading(1.2f)
                );
                if (line.isEmpty()) {
                    document.add(div);
                    break;
                }
            }
        }
        document.add(div);
 
        //Close document
        document.close();
    }
 
}
C04E06_CustomParagraph.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.chapter04;
 
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.kernel.pdf.canvas.CanvasArtifact;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.Background;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.ParagraphRenderer;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E06_CustomParagraph {
 
    class MyParagraphRenderer extends ParagraphRenderer {
 
        public MyParagraphRenderer(Paragraph modelElement) {
            super(modelElement);
        }
 
        @Override
        public void drawBackground(DrawContext drawContext) {
            Background background = this.<Background>getProperty(Property.BACKGROUND);
            if (background != null) {
                Rectangle bBox = getOccupiedAreaBBox();
                boolean isTagged =
                    drawContext.isTaggingEnabled();
                if (isTagged) {
                    drawContext.getCanvas().openTag(new CanvasArtifact());
                }
                Rectangle bgArea = applyMargins(bBox, false);
                if (bgArea.getWidth() <= 0 || bgArea.getHeight() <= 0) {
                    return;
                }
                drawContext.getCanvas().saveState()
                    .setFillColor(background.getColor())
                    .roundRectangle(
                    (double)bgArea.getX() - background.getExtraLeft(),
                    (double)bgArea.getY() - background.getExtraBottom(),
                    (double)bgArea.getWidth()
                        + background.getExtraLeft() + background.getExtraRight(),
                    (double)bgArea.getHeight()
                        + background.getExtraTop() + background.getExtraBottom(),
                    5)
                    .fill().restoreState();
                if (isTagged) {
                    drawContext.getCanvas().closeTag();
                }
            }
        }
    }
 
    public static final String DEST = "results/chapter04/custom_paragraph.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E06_CustomParagraph().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);
 
        Paragraph p1 = new Paragraph(
            "The Strange Case of Dr. Jekyll and Mr. Hyde");
        p1.setBackgroundColor(ColorConstants.ORANGE);
        document.add(p1);
 
        Paragraph p2 = new Paragraph(
            "The Strange Case of Dr. Jekyll and Mr. Hyde");
        p2.setBackgroundColor(ColorConstants.ORANGE);
        p2.setNextRenderer(new MyParagraphRenderer(p2));
        document.add(p2);
 
        document.close();
    }
}
C04E07_ListTypes.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
/*
    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.chapter04;
 
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.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.property.ListNumberingType;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E07_ListTypes {
    
    public static final String DEST = "results/chapter04/list_types.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E07_ListTypes().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;
 
        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));    
 
        List list = new List();
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.DECIMAL);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ENGLISH_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ENGLISH_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.GREEK_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.GREEK_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ROMAN_LOWER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ROMAN_UPPER);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_1);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_2);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_3);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        
        list = new List(ListNumberingType.ZAPF_DINGBATS_4);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
        //Close document
        document.close();
    }
}
C04E08_CustomListSymbols.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
/*
    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.chapter04;
 
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
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.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.property.ListNumberingType;
import com.itextpdf.layout.property.ListSymbolAlignment;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E08_CustomListSymbols {
 
    public static final String DEST = "results/chapter04/custom_list_symbols.pdf";
    public static final String INFO = "src/main/resources/img/test/info.png";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E08_CustomListSymbols().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
 
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;
 
        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));
 
        List list = new List();
        list.setListSymbol("\u2022");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        list = new List();
        PdfFont font = PdfFontFactory.createFont(StandardFonts.ZAPFDINGBATS);
        list.setListSymbol(new Text("*").setFont(font).setFontColor(ColorConstants.ORANGE));
        list.setSymbolIndent(10);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        Image info = new Image(ImageDataFactory.create(INFO));
        info.scaleAbsolute(12, 12);
        list = new List().setSymbolIndent(3);
        list.setListSymbol(info);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        list = new List();
        list.setListSymbol(ListNumberingType.ENGLISH_LOWER);
        list.setPostSymbolText("- ");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        list = new List(ListNumberingType.DECIMAL);
        list.setPreSymbolText("Part ");
        list.setPostSymbolText(": ");
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        list = new List(ListNumberingType.DECIMAL);
        list.setItemStartIndex(5);
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
        document.add(list);
 
        list = new List(ListNumberingType.ROMAN_LOWER);
        list.setListSymbolAlignment(ListSymbolAlignment.LEFT);
        for (int i = 0; i < 6; i++) {
            list.add("Dr. Jekyll");
            list.add("Mr. Hyde");
        }
        document.add(list);
        //Close document
        document.close();
    }
}
C04E09_ListItemExample.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
/*
    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.chapter04;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.ListNumberingType;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E09_ListItemExample {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter04/jekyll_hyde_overviewV4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E09_ListItemExample().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        resultSet.remove(0);
        com.itextpdf.layout.element.List list =
            new com.itextpdf.layout.element.List(ListNumberingType.DECIMAL);
        for (List<String> record : resultSet) {
            ListItem li = new ListItem();
            li.setKeepTogether(true);
            String url = String.format(
                "http://www.imdb.com/title/tt%s", record.get(0));
            Link movie = new Link(record.get(2), PdfAction.createURI(url));
            li.add(new Paragraph(movie.setFontSize(14)))
                .add(new Paragraph(String.format(
                    "Directed by %s (%s, %s)",
                    record.get(3), record.get(4), record.get(1))));
            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.scaleToFit(10000, 120);
                li.add(img);
            }
            list.add(li);
        }
        document.add(list);
        document.close();
    }
    
}
C04E10_NestedLists.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
/*
    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.chapter04;
 
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.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.property.ListNumberingType;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C04E10_NestedLists {
    
    public static final String DEST = "results/chapter04/nested_list.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C04E10_NestedLists().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        PageSize pagesize = PageSize.A6.rotate();
        Document document = new Document(pdf, pagesize);
        //Set column parameters
        float offSet = 36;
        float gutter = 23;
        float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
        float columnHeight = pagesize.getHeight() - offSet * 2;
 
        //Define column areas
        Rectangle[] columns = {
            new Rectangle(offSet, offSet, columnWidth, columnHeight),
            new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));    
 
        List list = new List();
        
        List list1 = new List(ListNumberingType.DECIMAL);
        List listEL = new List(ListNumberingType.ENGLISH_LOWER);
        listEL.add("Dr. Jekyll");
        listEL.add("Mr. Hyde");
        ListItem liEL = new ListItem();
        liEL.add(listEL);
        list1.add(liEL);
        List listEU = new List(ListNumberingType.ENGLISH_UPPER);
        listEU.add("Dr. Jekyll");
        listEU.add("Mr. Hyde");
        ListItem liEU = new ListItem();
        liEU.add(listEU);
        list1.add(liEU);
        ListItem li1 = new ListItem();
        li1.add(list1);
        list.add(li1);
        
        ListItem li = new ListItem();
        List listGL = new List(ListNumberingType.GREEK_LOWER);
        listGL.add("Dr. Jekyll");
        listGL.add("Mr. Hyde");
        li.add(listGL);
        List listGU = new List(ListNumberingType.GREEK_UPPER);
        listGU.add("Dr. Jekyll");
        listGU.add("Mr. Hyde");
        li.add(listGU);
        List listRL = new List(ListNumberingType.ROMAN_LOWER);
        listRL.add("Dr. Jekyll");
        listRL.add("Mr. Hyde");
        li.add(listRL);
        List listRU = new List(ListNumberingType.ROMAN_UPPER);
        listRU.add("Dr. Jekyll");
        listRU.add("Mr. Hyde");
        li.add(listRU);
        list.add(li);
        
        List listZ1 = new List(ListNumberingType.ZAPF_DINGBATS_1);
        listZ1.add("Dr. Jekyll");
        listZ1.add("Mr. Hyde");
        ListItem liZ1 = new ListItem();
        liZ1.add(listZ1);
        
        List listZ2 = new List(ListNumberingType.ZAPF_DINGBATS_2);
        listZ2.add("Dr. Jekyll");
        listZ2.add("Mr. Hyde");
        ListItem liZ2 = new ListItem();
        liZ2.add(listZ2);
        
        List listZ3 = new List(ListNumberingType.ZAPF_DINGBATS_3);
        listZ3.add("Dr. Jekyll");
        listZ3.add("Mr. Hyde");
        ListItem liZ3 = new ListItem();
        liZ3.add(listZ3);
        
        List listZ4 = new List(ListNumberingType.ZAPF_DINGBATS_4);
        listZ4.add("Dr. Jekyll");
        listZ4.add("Mr. Hyde");
        ListItem liZ4 = new ListItem();
        liZ4.add(listZ4);
        
        listZ3.add(liZ4);
        listZ2.add(liZ3);
        listZ1.add(liZ2);
        list.add(liZ1);
        document.add(list);
        //Close document
        document.close();
    }
}
C04E01_DivExample1.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.IO.Image;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E01_DivExample1 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter04/jekyll_hyde_overviewV1.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E01_DivExample1().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            foreach (IList<String> record in resultSet) {
                Div div = new Div().SetBorderLeft(new SolidBorder(2)).SetPaddingLeft(3).SetMarginBottom(10);
                String url = String.Format("http://www.imdb.com/title/tt{0}", record[0]);
                Link movie = new Link(record[2], PdfAction.CreateURI(url));
                div.Add(new Paragraph(movie.SetFontSize(14))).Add(new Paragraph(String.Format("Directed by {0} ({1}, {2})", record
                    [3], record[4], record[1])));
                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.ScaleToFit(10000, 120);
                    div.Add(img);
                }
                document.Add(div);
            }
            document.Close();
        }
    }
}
C04E02_DivExample2.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.IO.Image;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E02_DivExample2 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter04/jekyll_hyde_overviewV2.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E02_DivExample2().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            foreach (IList<String> record in resultSet) {
                Div div = new Div().SetKeepTogether(true).SetBorderLeft(new SolidBorder(2)).SetPaddingLeft(3).SetMarginBottom
                    (10);
                String url = String.Format("http://www.imdb.com/title/tt{0}", record[0]);
                Link movie = new Link(record[2], PdfAction.CreateURI(url));
                div.Add(new Paragraph(movie.SetFontSize(14))).Add(new Paragraph(String.Format("Directed by {0} ({1}, {2})", record
                    [3], record[4], record[1])));
                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.ScaleToFit(10000, 120);
                    div.Add(img);
                }
                document.Add(div);
            }
            document.Close();
        }
    }
}
C04E03_LineSeparatorExample.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
/*
* 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.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E03_LineSeparatorExample {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter04/jekyll_hyde_overviewV3.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E03_LineSeparatorExample().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            SolidLine line = new SolidLine(1f);
            line.SetColor(ColorConstants.RED);
            LineSeparator ls = new LineSeparator(line);
            ls.SetWidth(UnitValue.CreatePercentValue(50));
            ls.SetMarginTop(5);
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            foreach (IList<String> record in resultSet) {
                String url = String.Format("http://www.imdb.com/title/tt{0}", record[0]);
                Link movie = new Link(record[2], PdfAction.CreateURI(url));
                Div div = new Div().SetKeepTogether(true).SetBorderLeft(new SolidBorder(2)).SetPaddingLeft(3).SetMarginBottom
                    (10).Add(new Paragraph(movie.SetFontSize(14f))).Add(new Paragraph(String.Format("Directed by {0} ({1}, {2})"
                    , record[3], record[4], record[1])));
                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.ScaleToFit(10000, 120);
                    div.Add(img);
                }
                div.Add(ls);
                document.Add(div);
            }
            document.Close();
        }
    }
}
C04E04_ParagraphAndDiv1.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
 
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Hyphenation;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E04_ParagraphAndDiv1 {
        public const String SRC = "../../resources/txt/jekyll_hyde.txt";
 
        public const String DEST = "../../results/chapter04/jekyll_hydeV1.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E04_ParagraphAndDiv1().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);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3));
            StreamReader sr = File.OpenText(SRC);
            String line;
            Div div = new Div();
            while ((line = sr.ReadLine()) != null) {
                Paragraph title = new Paragraph(line).SetFont(bold).SetFontSize(12).SetMarginBottom(0);
                div = new Div().Add(title).SetFont(font).SetFontSize(11).SetMarginBottom(18);
                while ((line = sr.ReadLine()) != null) {
                    div.Add(new Paragraph(line).SetMarginBottom(0).SetFirstLineIndent(36));
                    if (String.IsNullOrEmpty(line)) {
                        document.Add(div);
                        break;
                    }
                }
            }
            document.Add(div);
            //Close document
            document.Close();
        }
    }
}
C04E05_ParagraphAndDiv2.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
 
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Hyphenation;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E05_ParagraphAndDiv2 {
        public const String SRC = "../../resources/txt/jekyll_hyde.txt";
 
        public const String DEST = "../../results/chapter04/jekyll_hydeV2.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E05_ParagraphAndDiv2().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);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3));
            StreamReader sr = File.OpenText(SRC);
            String line;
            Div div = new Div();
            while ((line = sr.ReadLine()) != null) {
                document.Add(new Paragraph(line).SetFont(bold).SetFontSize(12).SetMarginBottom(0).SetKeepWithNext(true));
                div = new Div().SetFont(font).SetFontSize(11).SetMarginBottom(18);
                while ((line = sr.ReadLine()) != null) {
                    div.Add(new Paragraph(line).SetMarginBottom(0).SetFirstLineIndent(36));
                    if (String.IsNullOrEmpty(line)) {
                        document.Add(div);
                        break;
                    }
                }
            }
            document.Add(div);
            //Close document
            document.Close();
        }
    }
}
C04E05_ParagraphAndDiv3.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
 
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Hyphenation;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E05_ParagraphAndDiv3 {
        public const String SRC = "../../resources/txt/jekyll_hyde.txt";
 
        public const String DEST = "../../results/chapter04/jekyll_hydeV3.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E05_ParagraphAndDiv3().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);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3));
            StreamReader sr = File.OpenText(SRC);
            String line;
            Div div = new Div();
            while ((line = sr.ReadLine()) != null) {
                document.Add(new Paragraph(line).SetFont(bold).SetFontSize(12).SetMarginBottom(0).SetKeepWithNext(true));
                div = new Div().SetFont(font).SetFontSize(11).SetMarginBottom(18);
                while ((line = sr.ReadLine()) != null) {
                    div.Add(new Paragraph(line).SetMarginBottom(0).SetFirstLineIndent(36).SetMultipliedLeading(1.2f));
                    if (String.IsNullOrEmpty(line)) {
                        document.Add(div);
                        break;
                    }
                }
            }
            document.Add(div);
            //Close document
            document.Close();
        }
    }
}
C04E06_CustomParagraph.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
/*
* 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.Kernel.Pdf.Tagutils;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E06_CustomParagraph {
        internal class MyParagraphRenderer : ParagraphRenderer {
            public MyParagraphRenderer(Paragraph modelElement)
                : base(modelElement) {
            }
 
            public override void DrawBackground(DrawContext drawContext) {
                Background background = this.GetProperty<Background>(Property.BACKGROUND);
                if (background != null) {
                    Rectangle bBox = this.GetOccupiedAreaBBox();
                    bool isTagged = drawContext.IsTaggingEnabled();
                    if (isTagged) {
                        drawContext.GetCanvas().OpenTag(new CanvasArtifact());
                    }
                    Rectangle bgArea = this.ApplyMargins(bBox, false);
                    if (bgArea.GetWidth() <= 0 || bgArea.GetHeight() <= 0) {
                        return;
                    }
                    drawContext.GetCanvas().SaveState().SetFillColor(background.GetColor()).RoundRectangle((double)bgArea.GetX
                        () - background.GetExtraLeft(), (double)bgArea.GetY() - background.GetExtraBottom(), (double)bgArea.GetWidth
                        () + background.GetExtraLeft() + background.GetExtraRight(), (double)bgArea.GetHeight() + background.GetExtraTop
                        () + background.GetExtraBottom(), 5).Fill().RestoreState();
                    if (isTagged) {
                        drawContext.GetCanvas().CloseTag();
                    }
                }
            }
        }
 
        public const String DEST = "../../results/chapter04/custom_paragraph.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E06_CustomParagraph().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);
            Paragraph p1 = new Paragraph("The Strange Case of Dr. Jekyll and Mr. Hyde");
            p1.SetBackgroundColor(ColorConstants.ORANGE);
            document.Add(p1);
            Paragraph p2 = new Paragraph("The Strange Case of Dr. Jekyll and Mr. Hyde");
            p2.SetBackgroundColor(ColorConstants.ORANGE);
            p2.SetNextRenderer(new C04E06_CustomParagraph.MyParagraphRenderer(p2));
            document.Add(p2);
            document.Close();
        }
    }
}
C04E07_ListTypes.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
/*
* 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.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E07_ListTypes {
        public const String DEST = "../../results/chapter04/list_types.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E07_ListTypes().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            PageSize pagesize = PageSize.A6.Rotate();
            Document document = new Document(pdf, pagesize);
            //Set column parameters
            float offSet = 36;
            float gutter = 23;
            float columnWidth = (pagesize.GetWidth() - offSet * 2) / 2 - gutter;
            float columnHeight = pagesize.GetHeight() - offSet * 2;
            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle
                (offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            List list = new List();
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.DECIMAL);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ENGLISH_LOWER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ENGLISH_UPPER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.GREEK_LOWER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.GREEK_UPPER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ROMAN_LOWER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ROMAN_UPPER);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ZAPF_DINGBATS_1);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ZAPF_DINGBATS_2);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ZAPF_DINGBATS_3);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ZAPF_DINGBATS_4);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            //Close document
            document.Close();
        }
    }
}
C04E08_CustomListSymbols.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
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E08_CustomListSymbols {
        public const String DEST = "../../results/chapter04/custom_list_symbols.pdf";
 
        public const String INFO = "../../resources/img/test/info.png";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E08_CustomListSymbols().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            PageSize pagesize = PageSize.A6.Rotate();
            Document document = new Document(pdf, pagesize);
            //Set column parameters
            float offSet = 36;
            float gutter = 23;
            float columnWidth = (pagesize.GetWidth() - offSet * 2) / 2 - gutter;
            float columnHeight = pagesize.GetHeight() - offSet * 2;
            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle
                (offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            List list = new List();
            list.SetListSymbol("\u2022");
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List();
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.ZAPFDINGBATS);
            list.SetListSymbol(new Text("*").SetFont(font).SetFontColor(ColorConstants.ORANGE));
            list.SetSymbolIndent(10);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            iText.Layout.Element.Image info = new Image(ImageDataFactory.Create(INFO));
            info.ScaleAbsolute(12, 12);
            list = new List().SetSymbolIndent(3);
            list.SetListSymbol(info);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List();
            list.SetListSymbol(ListNumberingType.ENGLISH_LOWER);
            list.SetPostSymbolText("- ");
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.DECIMAL);
            list.SetPreSymbolText("Part ");
            list.SetPostSymbolText(": ");
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.DECIMAL);
            list.SetItemStartIndex(5);
            list.Add("Dr. Jekyll");
            list.Add("Mr. Hyde");
            document.Add(list);
            list = new List(ListNumberingType.ROMAN_LOWER);
            list.SetListSymbolAlignment(ListSymbolAlignment.LEFT);
            for (int i = 0; i < 6; i++) {
                list.Add("Dr. Jekyll");
                list.Add("Mr. Hyde");
            }
            document.Add(list);
            //Close document
            document.Close();
        }
    }
}
C04E09_ListItemExample.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
/*
* 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.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E09_ListItemExample {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter04/jekyll_hyde_overviewV4.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E09_ListItemExample().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            resultSet.RemoveAt(0);
            List list = new List(ListNumberingType.DECIMAL);
            foreach (IList<String> record in resultSet) {
                ListItem li = new ListItem();
                li.SetKeepTogether(true);
                String url = String.Format("http://www.imdb.com/title/tt{0}", record[0]);
                Link movie = new Link(record[2], PdfAction.CreateURI(url));
                li.Add(new Paragraph(movie.SetFontSize(14))).Add(new Paragraph(String.Format("Directed by {0} ({1}, {2})", record
                    [3], record[4], record[1])));
                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.ScaleToFit(10000, 120);
                    li.Add(img);
                }
                list.Add(li);
            }
            document.Add(list);
            document.Close();
        }
    }
}
C04E10_NestedLists.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.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter04 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C04E10_NestedLists {
        public const String DEST = "../../results/chapter04/nested_list.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C04E10_NestedLists().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            PageSize pagesize = PageSize.A6.Rotate();
            Document document = new Document(pdf, pagesize);
            //Set column parameters
            float offSet = 36;
            float gutter = 23;
            float columnWidth = (pagesize.GetWidth() - offSet * 2) / 2 - gutter;
            float columnHeight = pagesize.GetHeight() - offSet * 2;
            //Define column areas
            Rectangle[] columns = new Rectangle[] { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle
                (offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            List list = new List();
            List list1 = new List(ListNumberingType.DECIMAL);
            List listEL = new List(ListNumberingType.ENGLISH_LOWER);
            listEL.Add("Dr. Jekyll");
            listEL.Add("Mr. Hyde");
            ListItem liEL = new ListItem();
            liEL.Add(listEL);
            list1.Add(liEL);
            List listEU = new List(ListNumberingType.ENGLISH_UPPER);
            listEU.Add("Dr. Jekyll");
            listEU.Add("Mr. Hyde");
            ListItem liEU = new ListItem();
            liEU.Add(listEU);
            list1.Add(liEU);
            ListItem li1 = new ListItem();
            li1.Add(list1);
            list.Add(li1);
            ListItem li = new ListItem();
            List listGL = new List(ListNumberingType.GREEK_LOWER);
            listGL.Add("Dr. Jekyll");
            listGL.Add("Mr. Hyde");
            li.Add(listGL);
            List listGU = new List(ListNumberingType.GREEK_UPPER);
            listGU.Add("Dr. Jekyll");
            listGU.Add("Mr. Hyde");
            li.Add(listGU);
            List listRL = new List(ListNumberingType.ROMAN_LOWER);
            listRL.Add("Dr. Jekyll");
            listRL.Add("Mr. Hyde");
            li.Add(listRL);
            List listRU = new List(ListNumberingType.ROMAN_UPPER);
            listRU.Add("Dr. Jekyll");
            listRU.Add("Mr. Hyde");
            li.Add(listRU);
            list.Add(li);
            List listZ1 = new List(ListNumberingType.ZAPF_DINGBATS_1);
            listZ1.Add("Dr. Jekyll");
            listZ1.Add("Mr. Hyde");
            ListItem liZ1 = new ListItem();
            liZ1.Add(listZ1);
            List listZ2 = new List(ListNumberingType.ZAPF_DINGBATS_2);
            listZ2.Add("Dr. Jekyll");
            listZ2.Add("Mr. Hyde");
            ListItem liZ2 = new ListItem();
            liZ2.Add(listZ2);
            List listZ3 = new List(ListNumberingType.ZAPF_DINGBATS_3);
            listZ3.Add("Dr. Jekyll");
            listZ3.Add("Mr. Hyde");
            ListItem liZ3 = new ListItem();
            liZ3.Add(listZ3);
            List listZ4 = new List(ListNumberingType.ZAPF_DINGBATS_4);
            listZ4.Add("Dr. Jekyll");
            listZ4.Add("Mr. Hyde");
            ListItem liZ4 = new ListItem();
            liZ4.Add(listZ4);
            listZ3.Add(liZ4);
            listZ2.Add(liZ3);
            listZ1.Add(liZ2);
            list.Add(liZ1);
            document.Add(list);
            //Close document
            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