移除任何间距、填充和边距,以便使用 iText 7 完全填充单元格。

3
如何在iText 7中创建一个没有任何间距、填充或内边距的单元格表格。当我将图片放入单元格中时,它需要填充单元格到100%,与边框接触,没有任何空间。
我尝试将以下内容添加到我的单元格:
Cell qrImageCell = new Cell();
qrImageCell.setMargin(0f);
qrImageCell.setPadding(0f);
qrImageCell.setHeight(44f);
qrImageCell.add(barcodeImage.setMargins(0f,0f,0f,0f).setPadding(0f));

我也尝试在表格本身中设置它:
table.setPadding(0f);
table.setMargin(0f);

但不管我尝试什么,二维码和边框之间总是有一个白色矩形区域(FYI:一旦它起作用,我当然会删除边框)。

enter image description here

2个回答

4

使用iText 7.1.15进行测试

使用以下图片:

enter image description here

结合以下代码:

Table table = new Table(1);
Image barcodeImage = new Image(ImageDataFactory.create("image.png"));
Cell qrImageCell = new Cell();
qrImageCell.setBorder(new SolidBorder(ColorConstants.RED, 2));
qrImageCell.setPadding(0f);
qrImageCell.add(barcodeImage);
table.addCell(qrImageCell);
doc.add(table);

结果输出如下:

输出结果为:

输入图像描述

可能的原因

高度不正确

在代码中,您使用了qrImageCell.setHeight(44f)。也许这个高度与图像的高度不匹配。然而,这不太可能是问题的原因,因为它不会增加单元格的宽度。

其他单元格的影响

将测试表格设置为具有两列,并添加几个单元格,可以看到同一行和同一列中的单元格大小将影响该单元格的大小。

table.addCell(new Cell().add(new Paragraph("cell (1,2)")).setHeight(300));
table.addCell(new Cell().add(new Paragraph("cell (2,1)")).setWidth(300));
table.addCell(new Cell().add(new Paragraph("cell (2,2)")));

enter image description here

这也不太可能是问题所在,因为您的输出显示图像已经位于单元格中心。

没有空白区域

也许白色“边距”只是您条形码图像的一部分。在这种情况下,没有任何间距、边距或填充,但在视觉上看起来是这样。

验证您的输入图像或为单元格设置背景颜色,以便确定白色区域是图像的一部分还是单元格的一部分: qrImageCell.setBackgroundColor(ColorConstants.GREEN)


0
最后背景颜色的技巧对我帮助很大。最终由iText7生成的QR图像出现了一个白色边框。这是由以下代码生成的:
BarcodeQRCode qrCode = new BarcodeQRCode(text);
PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
Image barcodeImage = new Image(barcodeObject).setPadding(0f).scaleAbsolute(44f, 44f)

但是将填充设置为“0f”确实可以删除单元格中的任何填充。只需找出是否有可能删除QR代码周围的边框。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接