简体   繁体   中英

iText: List of images in a cell

I would like to create a table that has a list of dots. I don't know ahead of time how many dots I have, but if they overflow the cell, I want them to wrap, just like text would. My code is something like this:

PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{80});
table.setLockedWidth(true);
Phrase listOfDots = new Phrase();
for (int i = 0; i < 40; i++) {
  listOfDots.add(new Chunk(pdf.correct, 0, 0));
  listOfDots.add(new Chunk(" "));
}
table.addCell(listOfDots);
outerCell.addElement(table);

The dots wrap, like I expect, but they don't all have the same size. There are 7 rows of 5 dots each, and all 35 dots have the same size. The last row of 5 dots are roughly half the size of the others.

(I tried to post an image, but I'm not veteran enough on this site.)

Is there a way to make all the images the same size?

Please take a look at the ImagesInChunkInCell example. Instead of a bullet, I took the image of a light bulb. I was able to reproduce the problem you described, but as you can see in list_with_images.pdf , I was able to add one extra line:

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);

The extra line is:

image.setScaleToFitHeight(false);

This prevents that the image is scaled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM