Adding images to a table

This is a code example of iText PDF, discover more.

24th October 2015
admin-marketing

iText 5 is no longer being developed, except for security fixes. Click the following link to see an updated example for iText 7.

ImagesNextToEachOther.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
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/19700549/itextsharp-images-are-not-coming-next-to-one-another
 * 
 * We create a table with two columns and two cells.
 * This way, we can add two images next to each other.
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class ImagesNextToEachOther {
    public static final String DEST = "results/tables/images_next_to_each_other.pdf";
    public static final String IMG1 = "resources/images/javaone2013.jpg";
    public static final String IMG2 = "resources/images/berlin2013.jpg";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImagesNextToEachOther().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.addCell(createImageCell(IMG1));
        table.addCell(createImageCell(IMG2));
        document.add(table);
        document.close();
    }
    
    public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);
        return cell;
    }
}
IconDescriptionTable.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
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/23375618/how-to-add-an-icon-to-an-itext-pdfpcell
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class IconDescriptionTable {
    public static final String DEST = "results/tables/icon_description.pdf";
    public static final String IMG = "resources/images/bulb.gif";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new IconDescriptionTable().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[]{ 1, 9 });
        Image img = Image.getInstance(IMG);
        table.addCell(new PdfPCell(img, true));
        table.addCell(new Phrase("A light bulb icon"));
        document.add(table);
        document.close();
    }
}
ImageNextToText.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
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/25780258/how-to-display-image-and-text-beside-each-other-itext
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ImageNextToText {
    public static final String DEST = "results/tables/image_next_to_text.pdf";
    public static final String IMG1 = "resources/images/javaone2013.jpg";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImageNextToText().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.setWidths(new int[]{1, 2});
        table.addCell(createImageCell(IMG1));
        table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));
        document.add(table);
        document.close();
    }
    
    public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);
        return cell;
    }
    
    public static PdfPCell createTextCell(String text) throws DocumentException, IOException {
        PdfPCell cell = new PdfPCell();
        Paragraph p = new Paragraph(text);
        p.setAlignment(Element.ALIGN_RIGHT);
        cell.addElement(p);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setBorder(Rectangle.NO_BORDER);
        return cell;
    }
}
ImagesInChunkInCell.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
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/32130219/itext-list-of-images-in-a-cell
 */
package sandbox.tables;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 * @author Bruno Lowagie
 */
@WrapToTest
public class ImagesInChunkInCell {
    public static final String IMG = "resources/images/bulb.gif";
    public static final String DEST = "results/tables/list_with_images.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImagesInChunkInCell().createPdf(DEST);
    }
        
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Image image = Image.getInstance(IMG);
        image.setScaleToFitHeight(false);
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(new float[]{120});
        table.setLockedWidth(true);
        Phrase listOfDots = new Phrase();
        for (int i = 0; i < 40; i++) {
            listOfDots.add(new Chunk(image, 0, 0));
            listOfDots.add(new Chunk(" "));
        }
        table.addCell(listOfDots);
        document.add(table);
        document.close();
    }
}
SimpleTable8.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
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29752875/generate-pdf-from-java-code-that-dynamically-continue-file-template-with-itext
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class SimpleTable8 {
    public static final String DEST = "results/tables/simple_table8.pdf";
    
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable8().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);
        PdfReader reader = new PdfReader("resources/pdfs/header.pdf");
        PdfImportedPage header = writer.getImportedPage(reader, 1);
        PdfPCell cell = new PdfPCell(Image.getInstance(header));
        cell.setColspan(3);
        table.addCell(cell);
        for (int row = 1; row <= 50; row++) {
            for (int column = 1; column <= 3; column++) {
                table.addCell(String.format("row %s, column %s", row, column));
            }
        }
        reader = new PdfReader("resources/pdfs/footer.pdf");
        PdfImportedPage footer = writer.getImportedPage(reader, 1);
        cell = new PdfPCell(Image.getInstance(footer));
        cell.setColspan(3);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
PositionContentInCell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class PositionContentInCell {
    public static final String DEST = "results/tables/position_content_in_cell.pdf";
    public static final String IMG = "resources/images/info.png";
    public enum POSITION { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
    
    class ImageEvent implements PdfPCellEvent {
        protected Image img;
        public ImageEvent(Image img) {
            this.img = img;
        }
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            img.scaleToFit(position.getWidth(), position.getHeight());
            img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth()) / 2,
                    position.getBottom() + (position.getHeight() - img.getScaledHeight()) / 2);
            PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
            try {
                canvas.addImage(img);
            } catch (DocumentException ex) {
                // do nothing
            }
        }
    }
    
    class PositionEvent implements PdfPCellEvent {
        protected Phrase content;
        protected POSITION pos;
        
        public PositionEvent(Phrase content, POSITION pos) {
            this.content = content;
            this.pos = pos;
        }
        
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            float x = 0;
            float y = 0;
            int alignment = 0;
            switch (pos) {
                case TOP_LEFT:
                    x = position.getLeft(3);
                    y = position.getTop(content.getLeading());
                    alignment = Element.ALIGN_LEFT;
                    break;
                case TOP_RIGHT:
                    x = position.getRight(3);
                    y = position.getTop(content.getLeading());
                    alignment = Element.ALIGN_RIGHT;
                    break;
                case BOTTOM_LEFT:
                    x = position.getLeft(3);
                    y = position.getBottom(3);
                    alignment = Element.ALIGN_LEFT;
                    break;
                case BOTTOM_RIGHT:
                    x = position.getRight(3);
                    y = position.getBottom(3);
                    alignment = Element.ALIGN_RIGHT;
                    break;
            }
            ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
        }
    }
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PositionContentInCell().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        // 1. Create a Document which contains a table:
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell();
        PdfPCell cell2 = new PdfPCell();
        PdfPCell cell3 = new PdfPCell();
        PdfPCell cell4 = new PdfPCell();
        // 2. Inside that table, make each cell with specific height:
        cell1.setFixedHeight(50);
        cell2.setFixedHeight(50);
        cell3.setFixedHeight(50);
        cell4.setFixedHeight(50);
        // 3. Each cell has the same background image
        ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
        cell1.setCellEvent(imgEvent);
        cell2.setCellEvent(imgEvent);
        cell3.setCellEvent(imgEvent);
        cell4.setCellEvent(imgEvent);
        // 4. Add text in front of the image at specific position
        cell1.setCellEvent(new PositionEvent(new Phrase("Top left"), POSITION.TOP_LEFT));
        cell2.setCellEvent(new PositionEvent(new Phrase("Top right"), POSITION.TOP_RIGHT));
        cell3.setCellEvent(new PositionEvent(new Phrase("Bottom left"), POSITION.BOTTOM_LEFT));
        cell4.setCellEvent(new PositionEvent(new Phrase("Bottom right"), POSITION.BOTTOM_RIGHT));
        // Wrap it all up!
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        document.add(table);
        document.close();
    }
}
PositionContentInCell2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class PositionContentInCell2 {
    public static final String DEST = "results/tables/position_content_in_cell2.pdf";
    public static final String IMG = "resources/images/info.png";
    
    class ImageEvent implements PdfPCellEvent {
        protected Image img;
        public ImageEvent(Image img) {
            this.img = img;
        }
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            img.scaleAbsolute(position.getWidth(), position.getHeight());
            img.setAbsolutePosition(position.getLeft(), position.getBottom());
            PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
            try {
                canvas.addImage(img);
            } catch (DocumentException ex) {
                // do nothing
            }
        }
    }
    
    class PositionEvent implements PdfPCellEvent {
        protected Phrase content;
        protected float wPct;
        protected float hPct;
        protected int alignment;
        
        public PositionEvent(Phrase content, float wPct, float hPct, int alignment) {
            this.content = content;
            this.wPct = wPct;
            this.hPct = hPct;
            this.alignment = alignment;
        }
        
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            float x = position.getLeft() + wPct * position.getWidth();
            float y = position.getBottom() + hPct * (position.getHeight() - content.getLeading());
            ColumnText.showTextAligned(canvas, alignment, content, x, y, 0);
        }
    }
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PositionContentInCell2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        // 1. Create a Document which contains a table:
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell();
        PdfPCell cell2 = new PdfPCell();
        PdfPCell cell3 = new PdfPCell();
        PdfPCell cell4 = new PdfPCell();
        PdfPCell cell5 = new PdfPCell();
        PdfPCell cell6 = new PdfPCell();
        PdfPCell cell7 = new PdfPCell();
        PdfPCell cell8 = new PdfPCell();
        // 2. Inside that table, make each cell with specific height:
        cell1.setFixedHeight(50);
        cell2.setFixedHeight(50);
        cell3.setFixedHeight(50);
        cell4.setFixedHeight(50);
        cell5.setFixedHeight(50);
        cell6.setFixedHeight(50);
        cell7.setFixedHeight(50);
        cell8.setFixedHeight(50);
        // 3. Each cell has the same background image
        ImageEvent imgEvent = new ImageEvent(Image.getInstance(IMG));
        cell1.setCellEvent(imgEvent);
        cell2.setCellEvent(imgEvent);
        cell3.setCellEvent(imgEvent);
        cell4.setCellEvent(imgEvent);
        cell5.setCellEvent(imgEvent);
        cell6.setCellEvent(imgEvent);
        cell7.setCellEvent(imgEvent);
        cell8.setCellEvent(imgEvent);
        // 4. Add text in front of the image at specific position
        cell1.setCellEvent(new PositionEvent(new Phrase(14, "Top left"), 0, 1, Element.ALIGN_LEFT));
        cell2.setCellEvent(new PositionEvent(new Phrase(14, "Top right"), 1, 1, Element.ALIGN_RIGHT));
        cell3.setCellEvent(new PositionEvent(new Phrase(14, "Top center"), 0.5f, 1, Element.ALIGN_CENTER));
        cell4.setCellEvent(new PositionEvent(new Phrase(14, "Bottom center"), 0.5f, 0, Element.ALIGN_CENTER));
        cell5.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
        cell6.setCellEvent(new PositionEvent(new Phrase(14, "Middle center"), 0.5f, 0.5f, Element.ALIGN_CENTER));
        cell7.setCellEvent(new PositionEvent(new Phrase(14, "Bottom left"), 0, 0, Element.ALIGN_LEFT));
        cell8.setCellEvent(new PositionEvent(new Phrase(14, "Bottom right"), 1, 0, Element.ALIGN_RIGHT));
        // Wrap it all up!
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);
        table.addCell(cell5);
        table.addCell(cell6);
        table.addCell(cell7);
        table.addCell(cell8);
        document.add(table);
        document.close();
    }
}
AddOverlappingImage.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
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/22094289/itext-precisely-position-an-image-on-top-of-a-pdfptable
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class AddOverlappingImage {
    
    public static final String DEST = "results/tables/add_overlapping_image.pdf";
 
    public class ImageContent implements PdfPTableEvent {
        protected Image content;
        public ImageContent(Image content) {
            this.content = content;
        }
        public void tableLayout(PdfPTable table, float[][] widths,
                float[] heights, int headerRows, int rowStart,
                PdfContentByte[] canvases) {
            try {
                PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
                float x = widths[3][1] + 10;
                float y = heights[3] - 10 - content.getScaledHeight();
                content.setAbsolutePosition(x, y);
                canvas.addImage(content);
            } catch (DocumentException e) {
                throw new ExceptionConverter(e);
            }
        }
    }
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddOverlappingImage().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(5);
        table.setTableEvent(new ImageContent(Image.getInstance("resources/images/hero.jpg")));
        table.setWidthPercentage(100);
        PdfPCell cell;
        for (int r = 'A'; r <= 'Z'; r++) {
            for (int c = 1; c <= 5; c++) {
                cell = new PdfPCell();
                cell.addElement(new Paragraph(String.valueOf((char) r) + String.valueOf(c)));
                table.addCell(cell);
            }
        }
        document.add(table);
        document.close();
    }
}
MultipleImagesInCell.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
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/33603296/how-to-add-two-images-in-one-cell-in-itext
 */
package sandbox.tables;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class MultipleImagesInCell {
    
    public static final String DEST = "results/tables/images_in_cell.pdf";
    public static final String IMG1 = "resources/images/brasil.png";
    public static final String IMG2 = "resources/images/dog.bmp";
    public static final String IMG3 = "resources/images/fox.bmp";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new MultipleImagesInCell().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Image img1 = Image.getInstance(IMG1);
        Image img2 = Image.getInstance(IMG2);
        Image img3 = Image.getInstance(IMG3);
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(50);
        table.addCell("Different images, one after the other vertically:");
        PdfPCell cell = new PdfPCell();
        cell.addElement(img1);
        cell.addElement(img2);
        cell.addElement(img3);
        table.addCell(cell);
        document.add(table);
        document.newPage();
        table = new PdfPTable(1);
        table.addCell("Different images, one after the other vertically, but scaled:");
        cell = new PdfPCell();
        img1.setWidthPercentage(20);
        cell.addElement(img1);
        img2.setWidthPercentage(20);
        cell.addElement(img2);
        img3.setWidthPercentage(20);
        cell.addElement(img3);
        table.addCell(cell);
        table.addCell("Different images, one after the other horizontally:");
        Paragraph p = new Paragraph();
        img1.scalePercent(30);
        p.add(new Chunk(img1, 0, 0, true));
        p.add(new Chunk(img2, 0, 0, true));
        p.add(new Chunk(img3, 0, 0, true));
        cell = new PdfPCell();
        cell.addElement(p);
        table.addCell(cell);
        table.addCell("Text and images (mixed):");
        p = new Paragraph("The quick brown ");
        p.add(new Chunk(img3, 0, 0, true));
        p.add(" jumps over the lazy ");
        p.add(new Chunk(img2, 0, 0, true));
        cell = new PdfPCell();
        cell.addElement(p);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
MultipleImagesInTable.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
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34303448
 */
package sandbox.tables;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class MultipleImagesInTable {
    
    public static final String DEST = "results/tables/images_in_table_sequence.pdf";
    public static final String IMG1 = "resources/images/brasil.png";
    public static final String IMG2 = "resources/images/dog.bmp";
    public static final String IMG3 = "resources/images/fox.bmp";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new MultipleImagesInTable().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Image img1 = Image.getInstance(IMG1);
        Image img2 = Image.getInstance(IMG2);
        Image img3 = Image.getInstance(IMG3);
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(20);
        table.addCell(img1);
        table.addCell("Brazil");
        table.addCell(img2);
        table.addCell("Dog");
        table.addCell(img3);
        table.addCell("Fox");
        document.add(table);
        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