iText pdf library
Website search

iText 7 Building Blocks - Chapter 3: ILeafElement examples

C03E01_JekyllHydeTabsV1.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-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.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E01_JekyllHydeTabsV1 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs1.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E01_JekyllHydeTabsV1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 1; i <= 10; i++) {
            pdfCanvas.moveTo(document.getLeftMargin() + i * 50, 0);
            pdfCanvas.lineTo(document.getLeftMargin() + i * 50, 595);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.add(record.get(0).trim()).add(new Tab())
                .add(record.get(1).trim()).add(new Tab())
                .add(record.get(2).trim()).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim());
            document.add(p);
        }
        document.close();
    }
}
C03E02_JekyllHydeTabsV2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * This example was written by Bruno Lowagie
 * in the context of the book: iText 7 building blocks
 */
package com.itextpdf.highlevel.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E02_JekyllHydeTabsV2 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs2.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E02_JekyllHydeTabsV2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        
        float[] stops = new float[]{80, 120, 430, 640, 720};
        List<TabStop> tabstops = new ArrayList();
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 0; i < stops.length; i++) {
            tabstops.add(new TabStop(stops[i]));
            pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
            pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 595);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            p.add(record.get(0).trim()).add(new Tab())
                .add(record.get(1).trim()).add(new Tab())
                .add(record.get(2).trim()).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim());
            document.add(p);
        }
        document.close();
    }
}
C03E03_JekyllHydeTabsV3.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
/*
    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.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.property.TabAlignment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E03_JekyllHydeTabsV3 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs3.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E03_JekyllHydeTabsV3().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        
        float[] stops = new float[]{80, 120, 580, 590, 720};
        List<TabStop> tabstops = new ArrayList();
        tabstops.add(new TabStop(stops[0], TabAlignment.CENTER));
        tabstops.add(new TabStop(stops[1], TabAlignment.LEFT));
        tabstops.add(new TabStop(stops[2], TabAlignment.RIGHT));
        tabstops.add(new TabStop(stops[3], TabAlignment.LEFT));
        TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR);
        anchor.setTabAnchor(' ');
        tabstops.add(anchor);
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 0; i < stops.length; i++) {
            pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
            pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 595);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            p.add(record.get(0).trim()).add(new Tab())
                .add(record.get(1).trim()).add(new Tab())
                .add(record.get(2).trim()).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim() + " \'");
            document.add(p);
        }
        document.close();
    }
}
C03E04_JekyllHydeTabsV4.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
/*
    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.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
import com.itextpdf.kernel.pdf.canvas.draw.DottedLine;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.property.TabAlignment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E04_JekyllHydeTabsV4 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E04_JekyllHydeTabsV4().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        
        float[] stops = new float[]{80, 120, 580, 590, 720};
        List<TabStop> tabstops = new ArrayList();
        tabstops.add(new TabStop(stops[0], TabAlignment.CENTER, new DottedLine()));
        tabstops.add(new TabStop(stops[1], TabAlignment.LEFT));
        tabstops.add(new TabStop(stops[2], TabAlignment.RIGHT, new SolidLine(0.5f)));
        tabstops.add(new TabStop(stops[3], TabAlignment.LEFT));
        TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR, new DashedLine());
        anchor.setTabAnchor(' ');
        tabstops.add(anchor);
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 0; i < stops.length; i++) {
            pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
            pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 595);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            p.add(record.get(0).trim()).add(new Tab())
                .add(record.get(1).trim()).add(new Tab())
                .add(record.get(2).trim()).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim() + " \'");
            document.add(p);
        }
        document.close();
    }
}
C03E05_JekyllHydeTabsV5.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
/*
    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.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.draw.DashedLine;
import com.itextpdf.kernel.pdf.canvas.draw.DottedLine;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.property.TabAlignment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E05_JekyllHydeTabsV5 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs5.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E05_JekyllHydeTabsV5().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4);
        
        float[] stops = new float[]{70, 90, 420, 430, 520};
        List<TabStop> tabstops = new ArrayList();
        tabstops.add(new TabStop(stops[0], TabAlignment.CENTER, new DottedLine()));
        tabstops.add(new TabStop(stops[1], TabAlignment.LEFT));
        tabstops.add(new TabStop(stops[2], TabAlignment.RIGHT, new SolidLine(0.5f)));
        tabstops.add(new TabStop(stops[3], TabAlignment.LEFT));
        TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR, new DashedLine());
        anchor.setTabAnchor(' ');
        tabstops.add(anchor);
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 0; i < stops.length; i++) {
            pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
            pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 842);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            p.add(record.get(0).trim()).add(new Tab())
                .add(record.get(1).trim()).add(new Tab())
                .add(record.get(2).trim()).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim() + " \'");
            document.add(p);
        }
        document.close();
    }
}
C03E06_JekyllHydeTabsV6.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
/*
    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.chapter03;
 
import com.itextpdf.highlevel.util.CsvTo2DList;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.property.TabAlignment;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author iText
 */
public class C03E06_JekyllHydeTabsV6 {
    
    public static final String SRC = "src/main/resources/data/jekyll_hyde.csv";
    public static final String DEST = "results/chapter03/jekyll_hyde_tabs6.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E06_JekyllHydeTabsV6().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf, PageSize.A4.rotate());
        
        float[] stops = new float[]{40, 580, 590, 720};
        List<TabStop> tabstops = new ArrayList();
        tabstops.add(new TabStop(stops[0], TabAlignment.LEFT));
        tabstops.add(new TabStop(stops[1], TabAlignment.RIGHT));
        tabstops.add(new TabStop(stops[2], TabAlignment.LEFT));
        TabStop anchor = new TabStop(stops[3], TabAlignment.ANCHOR);
        anchor.setTabAnchor(' ');
        tabstops.add(anchor);
        PdfCanvas pdfCanvas = new PdfCanvas(pdf.addNewPage());
        for (int i = 0; i < stops.length; i++) {
            pdfCanvas.moveTo(document.getLeftMargin() + stops[i], 0);
            pdfCanvas.lineTo(document.getLeftMargin() + stops[i], 595);
        }
        pdfCanvas.stroke();
        
        List<List<String>> resultSet = CsvTo2DList.convert(SRC, "|");
        for (List<String> record : resultSet) {
            Paragraph p = new Paragraph();
            p.addTabStops(tabstops);
            PdfAction uri = PdfAction.createURI(
                String.format("http://www.imdb.com/title/tt%s", record.get(0)));
            Link link = new Link(record.get(2).trim(), uri);
            p.add(record.get(1).trim()).add(new Tab())
                .add(link).add(new Tab())
                .add(record.get(3).trim()).add(new Tab())
                .add(record.get(4).trim()).add(new Tab())
                .add(record.get(5).trim() + " \'");
            document.add(p);
        }
        document.close();
    }
}
C03E07_TextExample.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
/*
    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.chapter03;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.File;
import java.io.IOException;
 
public class C03E07_TextExample {
    
    public static final String DEST = "results/chapter03/jekyll_hyde_text.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E07_TextExample().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Text t1 = new Text("The Strange Case of ");
        Text t2 = new Text("Dr. Jekyll").setTextRise(5);
        Text t3 = new Text(" and ").setHorizontalScaling(2);
        Text t4 = new Text("Mr. Hyde").setSkew(10, 45);
        document.add(new Paragraph(t1).add(t2).add(t3).add(t4));
        document.close();
    }
}
C03E08_MaryReillyV1.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
/*
    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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E08_MaryReillyV1 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V1.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E08_MaryReillyV1().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY));
        document.add(img);
        document.close();
    }
    
}
C03E09_MaryReillyV2.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
/*
    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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E09_MaryReillyV2 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V2.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E09_MaryReillyV2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY));
        document.add(img);
        document.add(img);
        document.close();
    }
    
}
C03E10_MaryReillyV3.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-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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E10_MaryReillyV3 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V3.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E10_MaryReillyV3().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img1 = new Image(ImageDataFactory.create(MARY));
        document.add(img1);
        Image img2 = new Image(ImageDataFactory.create(MARY));
        document.add(img2);
        document.close();
    }
    
}
C03E11_MaryReillyV4.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
/*
    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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E11_MaryReillyV4 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V4.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E11_MaryReillyV4().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY), 320, 750, 50);
        document.add(img);
        document.close();
    }
    
}
C03E12_MaryReillyV5.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-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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
public class C03E12_MaryReillyV5 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V5.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E12_MaryReillyV5().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY));
        img.setFixedPosition(320, 750, UnitValue.createPointValue(50));
        document.add(img);
        document.close();
    }
    
}
C03E13_MaryReillyV6.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-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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
public class C03E13_MaryReillyV6 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V6.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E13_MaryReillyV6().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY));
        img.setFixedPosition(2, 300, 750, UnitValue.createPointValue(50));
        document.add(img);
        document.close();
    }
    
}
C03E14_MaryReillyV7.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
/*
    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.chapter03;
 
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C03E14_MaryReillyV7 {
    public static final String SRC = "src/main/resources/pdfs/jekyll_hyde.pdf";
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V7.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E14_MaryReillyV7().manipulatePdf(SRC, DEST);
    }
    
    public void manipulatePdf(String src, String dest) throws IOException {
        PdfReader reader = new PdfReader(src);
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdfDoc = new PdfDocument(reader, writer);
        Document document = new Document(pdfDoc);
        Image img = new Image(ImageDataFactory.create(MARY));
        img.setFixedPosition(1, 350, 750, UnitValue.createPointValue(50));
        document.add(img);
        document.close();
    }
}
C03E15_MaryReillyV8.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
/*
    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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
import java.io.File;
import java.io.IOException;
 
public class C03E15_MaryReillyV8 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V8.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E15_MaryReillyV8().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        document.add(p);
        Image img = new Image(ImageDataFactory.create(MARY));
        img.setHorizontalAlignment(HorizontalAlignment.CENTER);
        img.setWidth(UnitValue.createPercentValue(80));
        document.add(img);
        document.close();
    }
    
}
C03E16_MaryReillyV9.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
/*
    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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E16_MaryReillyV9 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V9.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E16_MaryReillyV9().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        Image img = new Image(ImageDataFactory.create(MARY));
        p.add(img);
        document.add(p);
        document.close();
    }
    
}
C03E17_MaryReillyV10.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-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.chapter03;
 
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.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
 
public class C03E17_MaryReillyV10 {
    
    public static final String MARY = "src/main/resources/img/0117002.jpg";
    public static final String DEST = "results/chapter03/mary_reilly_V10.pdf";
       
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E17_MaryReillyV10().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Paragraph p = new Paragraph(
            "Mary Reilly is a maid in the household of Dr. Jekyll: ");
        Image img = new Image(ImageDataFactory.create(MARY));
        img.scale(0.5f, 0.5f);
        img.setRotationAngle(-Math.PI / 6);
        p.add(img);
        document.add(p);
        document.close();
    }
    
}
C03E18_ImageTypes.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
/*
    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.chapter03;
 
import com.itextpdf.io.image.ImageData;
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.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Image;
import java.awt.*;
import java.io.File;
import java.io.IOException;
 
/**
 * @author iText
 */
public class C03E18_ImageTypes {
    public static final String TEST1 = "src/main/resources/img/test/map.jp2";
    public static final String TEST2 = "src/main/resources/img/test/butterfly.bmp";
    public static final String TEST3 = "src/main/resources/img/test/hitchcock.png";
    public static final String TEST4 = "src/main/resources/img/test/info.png";
    public static final String TEST5 = "src/main/resources/img/test/hitchcock.gif";
    public static final String TEST6 = "src/main/resources/img/test/amb.jb2";
    public static final String TEST7 = "src/main/resources/img/test/marbles.tif";
 
    public static final String DEST = "results/chapter03/image_types.pdf";
 
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E18_ImageTypes().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
 
        // raw
        byte data[] = new byte[256 * 3];
        for (int i = 0; i < 256; i++) {
            data[i * 3] = (byte) (255 - i);
            data[i * 3 + 1] = (byte) (255 - i);
            data[i * 3 + 2] = (byte) i;
        }
        ImageData raw = ImageDataFactory.create(256, 1, 3, 8, data, null);
        Image img = new Image(raw);
        img.scaleAbsolute(256, 10);
        document.add(img);
 
        // JPEG2000
        Image img1 = new Image(ImageDataFactory.create(TEST1));
        document.add(img1);
        document.add(new AreaBreak());
 
        // BMP
        Image img2 = new Image(ImageDataFactory.create(TEST2));
        img2.setMarginBottom(10);
        document.add(img2);
        // PNG
        Image img3 = new Image(ImageDataFactory.create(TEST3));
        img3.setMarginBottom(10);
        document.add(img3);
        // Transparent PNG
        Image img4 = new Image(ImageDataFactory.create(TEST4));
        img4.setBorderLeft(new SolidBorder(6));
        document.add(img4);
 
        // GIF
        Image img5 = new Image(ImageDataFactory.create(TEST5));
        img5.setBackgroundColor(ColorConstants.LIGHT_GRAY);
        document.add(img5);
 
        // AWT
        java.awt.Image awtImage =
            Toolkit.getDefaultToolkit().createImage(TEST5);
        Image awt =
            new Image(ImageDataFactory.create(awtImage, java.awt.Color.yellow));
        awt.setMarginTop(10);
        document.add(awt);
 
        // JBIG2
        Image img6 = new Image(ImageDataFactory.create(TEST6));
        document.add(img6);
 
        // TIFF
        Image img7 = new Image(ImageDataFactory.create(TEST7));
        document.add(img7);
 
        document.close();
    }
}
C03E19_PagedImages.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
/*
    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.chapter03;
 
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.image.Jbig2ImageData;
import com.itextpdf.io.image.TiffImageData;
import com.itextpdf.io.source.IRandomAccessSource;
import com.itextpdf.io.source.RandomAccessFileOrArray;
import com.itextpdf.io.source.RandomAccessSourceFactory;
import com.itextpdf.io.util.UrlUtil;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
public class C03E19_PagedImages {
    public static final String TEST1 = "src/main/resources/img/test/animated_fox_dog.gif";
    public static final String TEST2 = "src/main/resources/img/test/amb.jb2";
    public static final String TEST3 = "src/main/resources/img/test/marbles.tif";
    public static final String DEST = "results/chapter03/paged_images.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E19_PagedImages().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        
        Image img;
        // Animated GIF
        URL url1 = UrlUtil.toURL(TEST1);
        List<ImageData> list = ImageDataFactory.createGifFrames(url1);
        for (ImageData data : list) {
            img = new Image(data);
            document.add(img);
        }
        
        // JBIG2
        URL url2 = UrlUtil.toURL(TEST2);
        IRandomAccessSource ras2 =
            new RandomAccessSourceFactory().createSource(url2);
        RandomAccessFileOrArray raf2 = new RandomAccessFileOrArray(ras2);
        int pages2 = Jbig2ImageData.getNumberOfPages(raf2);
        for (int i = 1; i <= pages2; i++) {
            img = new Image(ImageDataFactory.createJbig2(url2, i));
            document.add(img);
        }
            
        
        // TIFF
        URL url3 = UrlUtil.toURL(TEST3);
        IRandomAccessSource ras3 =
            new RandomAccessSourceFactory().createSource(url3);
        RandomAccessFileOrArray raf3 = new RandomAccessFileOrArray(ras3);
        int pages3 = TiffImageData.getNumberOfPages(raf3);
        for (int i = 1; i <= pages3; i++) {
            img = new Image(
                ImageDataFactory.createTiff(url3, true, i, true));
            document.add(img);
        }
        document.close();
    }
}
C03E20_XObjectTypes.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
/*
    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.chapter03;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.wmf.WmfImageData;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import java.io.File;
import java.io.IOException;
 
/**
 * @author iText
 */
public class C03E20_XObjectTypes {
    public static final String WMF = "src/main/resources/img/test/butterfly.wmf";
    public static final String SRC = "src/main/resources/pdfs/jekyll_hyde.pdf";
    
    public static final String DEST = "results/chapter03/xobject_types.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C03E20_XObjectTypes().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        PdfFormXObject xObject1 = new PdfFormXObject(new WmfImageData(WMF), pdf);
        Image img1 = new Image(xObject1);
        document.add(img1);
        PdfReader reader = new PdfReader(SRC);
        PdfDocument existing = new PdfDocument(reader);
        PdfPage page = existing.getPage(1);
        PdfFormXObject xObject2 = page.copyAsFormXObject(pdf);
        Image img2 = new Image(xObject2);
        img2.scaleToFit(400, 400);
        document.add(img2);
        document.close();
    }
}
C03E01_JekyllHydeTabsV1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
 
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E01_JekyllHydeTabsV1 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs1.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E01_JekyllHydeTabsV1().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 1; i <= 10; i++) {
                pdfCanvas.MoveTo(document.GetLeftMargin() + i * 50, 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + i * 50, 595);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.Add(record[0].Trim()).Add(new Tab()).Add(record[1].Trim()).Add(new Tab()).Add(record[2].Trim()).Add(new 
                    Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record[4].Trim()).Add(new Tab()).Add(record[5].Trim());
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E02_JekyllHydeTabsV2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
 
using System;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E02_JekyllHydeTabsV2 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs2.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E02_JekyllHydeTabsV2().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            float[] stops = new float[] { 80, 120, 430, 640, 720 };
            IList<TabStop> tabstops = new List<TabStop>();
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 0; i < stops.Length; i++) {
                tabstops.Add(new TabStop(stops[i]));
                pdfCanvas.MoveTo(document.GetLeftMargin() + stops[i], 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + stops[i], 595);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.AddTabStops(tabstops);
                p.Add(record[0].Trim()).Add(new Tab()).Add(record[1].Trim()).Add(new Tab()).Add(record[2].Trim()).Add(new 
                    Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record[4].Trim()).Add(new Tab()).Add(record[5].Trim());
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E03_JekyllHydeTabsV3.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;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E03_JekyllHydeTabsV3 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs3.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E03_JekyllHydeTabsV3().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            float[] stops = new float[] { 80, 120, 580, 590, 720 };
            IList<TabStop> tabstops = new List<TabStop>();
            tabstops.Add(new TabStop(stops[0], TabAlignment.CENTER));
            tabstops.Add(new TabStop(stops[1], TabAlignment.LEFT));
            tabstops.Add(new TabStop(stops[2], TabAlignment.RIGHT));
            tabstops.Add(new TabStop(stops[3], TabAlignment.LEFT));
            TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR);
            anchor.SetTabAnchor(' ');
            tabstops.Add(anchor);
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 0; i < stops.Length; i++) {
                pdfCanvas.MoveTo(document.GetLeftMargin() + stops[i], 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + stops[i], 595);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.AddTabStops(tabstops);
                p.Add(record[0].Trim()).Add(new Tab()).Add(record[1].Trim()).Add(new Tab()).Add(record[2].Trim()).Add(new 
                    Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record[4].Trim()).Add(new Tab()).Add(record[5].Trim() 
                    + " \'");
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E04_JekyllHydeTabsV4.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E04_JekyllHydeTabsV4 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs4.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E04_JekyllHydeTabsV4().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            float[] stops = new float[] { 80, 120, 580, 590, 720 };
            IList<TabStop> tabstops = new List<TabStop>();
            tabstops.Add(new TabStop(stops[0], TabAlignment.CENTER, new DottedLine()));
            tabstops.Add(new TabStop(stops[1], TabAlignment.LEFT));
            tabstops.Add(new TabStop(stops[2], TabAlignment.RIGHT, new SolidLine(0.5f)));
            tabstops.Add(new TabStop(stops[3], TabAlignment.LEFT));
            TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR, new DashedLine());
            anchor.SetTabAnchor(' ');
            tabstops.Add(anchor);
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 0; i < stops.Length; i++) {
                pdfCanvas.MoveTo(document.GetLeftMargin() + stops[i], 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + stops[i], 595);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.AddTabStops(tabstops);
                p.Add(record[0].Trim()).Add(new Tab()).Add(record[1].Trim()).Add(new Tab()).Add(record[2].Trim()).Add(new 
                    Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record[4].Trim()).Add(new Tab()).Add(record[5].Trim() 
                    + " \'");
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E05_JekyllHydeTabsV5.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E05_JekyllHydeTabsV5 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs5.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E05_JekyllHydeTabsV5().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4);
            float[] stops = new float[] { 70, 90, 420, 430, 520 };
            IList<TabStop> tabstops = new List<TabStop>();
            tabstops.Add(new TabStop(stops[0], TabAlignment.CENTER, new DottedLine()));
            tabstops.Add(new TabStop(stops[1], TabAlignment.LEFT));
            tabstops.Add(new TabStop(stops[2], TabAlignment.RIGHT, new SolidLine(0.5f)));
            tabstops.Add(new TabStop(stops[3], TabAlignment.LEFT));
            TabStop anchor = new TabStop(stops[4], TabAlignment.ANCHOR, new DashedLine());
            anchor.SetTabAnchor(' ');
            tabstops.Add(anchor);
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 0; i < stops.Length; i++) {
                pdfCanvas.MoveTo(document.GetLeftMargin() + stops[i], 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + stops[i], 842);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.AddTabStops(tabstops);
                p.Add(record[0].Trim()).Add(new Tab()).Add(record[1].Trim()).Add(new Tab()).Add(record[2].Trim()).Add(new 
                    Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record[4].Trim()).Add(new Tab()).Add(record[5].Trim() 
                    + " \'");
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E06_JekyllHydeTabsV6.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 example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using iText.Highlevel.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E06_JekyllHydeTabsV6 {
        public const String SRC = "../../resources/data/jekyll_hyde.csv";
 
        public const String DEST = "../../results/chapter03/jekyll_hyde_tabs6.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E06_JekyllHydeTabsV6().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf, PageSize.A4.Rotate());
            float[] stops = new float[] { 40, 580, 590, 720 };
            IList<TabStop> tabstops = new List<TabStop>();
            tabstops.Add(new TabStop(stops[0], TabAlignment.LEFT));
            tabstops.Add(new TabStop(stops[1], TabAlignment.RIGHT));
            tabstops.Add(new TabStop(stops[2], TabAlignment.LEFT));
            TabStop anchor = new TabStop(stops[3], TabAlignment.ANCHOR);
            anchor.SetTabAnchor(' ');
            tabstops.Add(anchor);
            PdfCanvas pdfCanvas = new PdfCanvas(pdf.AddNewPage());
            for (int i = 0; i < stops.Length; i++) {
                pdfCanvas.MoveTo(document.GetLeftMargin() + stops[i], 0);
                pdfCanvas.LineTo(document.GetLeftMargin() + stops[i], 595);
            }
            pdfCanvas.Stroke();
            IList<IList<String>> resultSet = CsvTo2DList.Convert(SRC, "|");
            foreach (IList<String> record in resultSet) {
                Paragraph p = new Paragraph();
                p.AddTabStops(tabstops);
                PdfAction uri = PdfAction.CreateURI(String.Format("http://www.imdb.com/title/tt{0}", record[0]));
                Link link = new Link(record[2].Trim(), uri);
                p.Add(record[1].Trim()).Add(new Tab()).Add(link).Add(new Tab()).Add(record[3].Trim()).Add(new Tab()).Add(record
                    [4].Trim()).Add(new Tab()).Add(record[5].Trim() + " \'");
                document.Add(p);
            }
            document.Close();
        }
    }
}
C03E07_TextExample.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
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E07_TextExample {
        public const String DEST = "../../results/chapter03/jekyll_hyde_text.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E07_TextExample().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Text t1 = new Text("The Strange Case of ");
            Text t2 = new Text("Dr. Jekyll").SetTextRise(5);
            Text t3 = new Text(" and ").SetHorizontalScaling(2);
            Text t4 = new Text("Mr. Hyde").SetSkew(10, 45);
            document.Add(new Paragraph(t1).Add(t2).Add(t3).Add(t4));
            document.Close();
        }
    }
}
C03E08_MaryReillyV1.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E08_MaryReillyV1 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V1.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E08_MaryReillyV1().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            document.Add(img);
            document.Close();
        }
    }
}
C03E09_MaryReillyV2.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E09_MaryReillyV2 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V2.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E09_MaryReillyV2().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            document.Add(img);
            document.Add(img);
            document.Close();
        }
    }
}
C03E10_MaryReillyV3.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E10_MaryReillyV3 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V3.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E10_MaryReillyV3().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img1 = new Image(ImageDataFactory.Create(MARY));
            document.Add(img1);
            iText.Layout.Element.Image img2 = new iText.Layout.Element.Image(ImageDataFactory.Create(MARY));
            document.Add(img2);
            document.Close();
        }
    }
}
C03E11_MaryReillyV4.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E11_MaryReillyV4 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V4.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E11_MaryReillyV4().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY), 320, 750, 50);
            document.Add(img);
            document.Close();
        }
    }
}
C03E12_MaryReillyV5.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E12_MaryReillyV5 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V5.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E12_MaryReillyV5().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            img.SetFixedPosition(320, 750, UnitValue.CreatePointValue(50));
            document.Add(img);
            document.Close();
        }
    }
}
C03E13_MaryReillyV6.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E13_MaryReillyV6 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V6.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E13_MaryReillyV6().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            img.SetFixedPosition(2, 300, 750, UnitValue.CreatePointValue(50));
            document.Add(img);
            document.Close();
        }
    }
}
C03E14_MaryReillyV7.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C03E14_MaryReillyV7 {
        public const String SRC = "../../resources/pdfs/jekyll_hyde.pdf";
 
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V7.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E14_MaryReillyV7().ManipulatePdf(SRC, DEST);
        }
 
        public virtual void ManipulatePdf(String src, String dest) {
            PdfReader reader = new PdfReader(src);
            PdfWriter writer = new PdfWriter(dest);
            PdfDocument pdfDoc = new PdfDocument(reader, writer);
            Document document = new Document(pdfDoc);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            img.SetFixedPosition(1, 350, 750, UnitValue.CreatePointValue(50));
            document.Add(img);
            document.Close();
        }
    }
}
C03E15_MaryReillyV8.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E15_MaryReillyV8 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V8.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E15_MaryReillyV8().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            document.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            img.SetHorizontalAlignment(HorizontalAlignment.CENTER);
            img.SetWidth(UnitValue.CreatePercentValue(80));
            document.Add(img);
            document.Close();
        }
    }
}
C03E16_MaryReillyV9.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E16_MaryReillyV9 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V9.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E16_MaryReillyV9().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            p.Add(img);
            document.Add(p);
            document.Close();
        }
    }
}
C03E17_MaryReillyV10.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
/*
* 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.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    public class C03E17_MaryReillyV10 {
        public const String MARY = "../../resources/img/0117002.jpg";
 
        public const String DEST = "../../results/chapter03/mary_reilly_V10.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E17_MaryReillyV10().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Paragraph p = new Paragraph("Mary Reilly is a maid in the household of Dr. Jekyll: ");
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(MARY));
            img.Scale(0.5f, 0.5f);
            img.SetRotationAngle(-Math.PI / 6);
            p.Add(img);
            document.Add(p);
            document.Close();
        }
    }
}
C03E18_ImageTypes.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
/*
* 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.Image;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E18_ImageTypes {
        public const String TEST1 = "../../resources/img/test/map.jp2";
 
        public const String TEST2 = "../../resources/img/test/butterfly.bmp";
 
        public const String TEST3 = "../../resources/img/test/hitchcock.png";
 
        public const String TEST4 = "../../resources/img/test/info.png";
 
        public const String TEST5 = "../../resources/img/test/hitchcock.gif";
 
        public const String TEST6 = "../../resources/img/test/amb.jb2";
 
        public const String TEST7 = "../../resources/img/test/marbles.tif";
 
        public const String DEST = "../../results/chapter03/image_types.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E18_ImageTypes().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            // raw
            byte[] data = new byte[256 * 3];
            for (int i = 0; i < 256; i++) {
                data[i * 3] = (byte)(255 - i);
                data[i * 3 + 1] = (byte)(255 - i);
                data[i * 3 + 2] = (byte)i;
            }
            ImageData raw = ImageDataFactory.Create(256, 1, 3, 8, data, null);
            iText.Layout.Element.Image img = new iText.Layout.Element.Image(raw);
            img.ScaleAbsolute(256, 10);
            document.Add(img);
            // JPEG2000
            iText.Layout.Element.Image img1 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST1));
            document.Add(img1);
            document.Add(new AreaBreak());
            // BMP
            iText.Layout.Element.Image img2 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST2));
            img2.SetMarginBottom(10);
            document.Add(img2);
            // PNG
            iText.Layout.Element.Image img3 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST3));
            img3.SetMarginBottom(10);
            document.Add(img3);
            // Transparent PNG
            iText.Layout.Element.Image img4 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST4));
            img4.SetBorderLeft(new SolidBorder(6));
            document.Add(img4);
            // GIF
            iText.Layout.Element.Image img5 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST5));
            img5.SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            document.Add(img5);
            
            // System.Drawing.Image
            System.Drawing.Image systemDrawingImage = System.Drawing.Image.FromFile(TEST5);
            iText.Layout.Element.Image awt = new iText.Layout.Element.Image(ImageDataFactory.Create(systemDrawingImage, System.Drawing.Color.Yellow));
            awt.SetMarginTop(10);
            document.Add(awt);
            
            // JBIG2
            iText.Layout.Element.Image img6 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST6));
            document.Add(img6);
            // TIFF
            iText.Layout.Element.Image img7 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST7));
            document.Add(img7);
            document.Close();
        }
    }
}
C03E19_PagedImages.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.IO.Image;
using iText.IO.Source;
using iText.IO.Util;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C03E19_PagedImages {
        public const String TEST1 = "../../resources/img/test/animated_fox_dog.gif";
 
        public const String TEST2 = "../../resources/img/test/amb.jb2";
 
        public const String TEST3 = "../../resources/img/test/marbles.tif";
 
        public const String DEST = "../../results/chapter03/paged_images.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E19_PagedImages().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Image img;
            // Animated GIF
            Uri url1 = UrlUtil.ToURL(TEST1);
            IList<ImageData> list = ImageDataFactory.CreateGifFrames(url1);
            foreach (ImageData data in list) {
                img = new iText.Layout.Element.Image(data);
                document.Add(img);
            }
            // JBIG2
            Uri url2 = UrlUtil.ToURL(TEST2);
            IRandomAccessSource ras2 = new RandomAccessSourceFactory().CreateSource(url2);
            RandomAccessFileOrArray raf2 = new RandomAccessFileOrArray(ras2);
            int pages2 = Jbig2ImageData.GetNumberOfPages(raf2);
            for (int i = 1; i <= pages2; i++) {
                img = new iText.Layout.Element.Image(ImageDataFactory.CreateJbig2(url2, i));
                document.Add(img);
            }
            // TIFF
            Uri url3 = UrlUtil.ToURL(TEST3);
            IRandomAccessSource ras3 = new RandomAccessSourceFactory().CreateSource(url3);
            RandomAccessFileOrArray raf3 = new RandomAccessFileOrArray(ras3);
            int pages3 = TiffImageData.GetNumberOfPages(raf3);
            for (int i_1 = 1; i_1 <= pages3; i_1++) {
                img = new iText.Layout.Element.Image(ImageDataFactory.CreateTiff(url3, true, i_1, true));
                document.Add(img);
            }
            document.Close();
        }
    }
}
C03E20_XObjectTypes.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
/*
* This example was written by Bruno Lowagie
* in the context of the book: iText 7 building blocks
*/
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Wmf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Highlevel.Chapter03 {
    /// <author>iText</author>
    public class C03E20_XObjectTypes {
        public const String WMF = "../../resources/img/test/butterfly.wmf";
 
        public const String SRC = "../../resources/pdfs/jekyll_hyde.pdf";
 
        public const String DEST = "../../results/chapter03/xobject_types.pdf";
 
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C03E20_XObjectTypes().CreatePdf(DEST);
        }
 
        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            PdfFormXObject xObject1 = new PdfFormXObject(new WmfImageData(WMF), pdf);
            Image img1 = new Image(xObject1);
            document.Add(img1);
            PdfReader reader = new PdfReader(SRC);
            PdfDocument existing = new PdfDocument(reader);
            PdfPage page = existing.GetPage(1);
            PdfFormXObject xObject2 = page.CopyAsFormXObject(pdf);
            Image img2 = new Image(xObject2);
            img2.ScaleToFit(400, 400);
            document.Add(img2);
            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