简体   繁体   English

如何在iText的一个单元格中添加两张图片?

[英]How to add two images in one cell in iText?

I'm currently creating a system that generates a PDF.我目前正在创建一个生成 PDF 的系统。 However, I cannot put two - three images in a single cell.但是,我不能在一个单元格中放置两到三个图像。 I tried for-looping it but it has borders.我尝试循环它,但它有边界。 What should i do?我该怎么办?

Please take a look at the ImagesInCell example.请看一下ImagesInCell示例。 It uses three images:它使用三个图像:

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

These are the image instances:这些是图像实例:

Image img1 = Image.getInstance(IMG1);
Image img2 = Image.getInstance(IMG2);
Image img3 = Image.getInstance(IMG3);

The easiest way to add multiple images to a single cell is by using addElement multiple times:将多个图像添加到单个单元格的最简单方法是多次使用addElement

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

The result looks like this:结果如下所示:

在此处输入图片说明

As you can see, the images were scaled automatically, to fit the width of the cell.如您所见,图像已自动缩放,以适应单元格的宽度。 If that isn't what you want, you have to improve your question, because you only claim that you can't add three images to the same cell, whereas this simple example proves the exact opposite.如果这不是你想要的,你必须改进你的问题,因为你只是声称你不能向同一个单元格添加三个图像,而这个简单的例子证明恰恰相反。

Maybe you want something that looks like this:也许你想要这样的东西:

在此处输入图片说明

In the first row with images, we use the same addElement() method as before, but we change the width percentage of the image to 20%:在第一行图像中,我们使用与之前相同的addElement()方法,但我们将图像的宽度百分比更改为 20%:

cell = new PdfPCell();
img1.setWidthPercentage(20);
cell.addElement(img1);
img2.setWidthPercentage(20);
cell.addElement(img2);
img3.setWidthPercentage(20);
cell.addElement(img3);
table.addCell(cell);

In the second row with images, we use a different approach: we have wrapped the images inside Chunk objects, so that we can put them next to each other:在带有图像的第二行中,我们使用了不同的方法:我们将图像包裹在Chunk对象中,以便我们可以将它们彼此相邻放置:

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

Observe that I scaled the first image.观察我缩放了第一张图像。 The three images wouldn't fit next to each other if that image kept its original size.如果该图像保持其原始大小,则这三个图像将无法并排放置。

Wrapping an image inside a Chunk has the advantage that we can mix images and text:将图像包裹在Chunk中的优点是我们可以混合图像和文本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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