Itext7 删除任何间距、填充和边距,使单元格完全充满iText 7

Itext7 删除任何间距、填充和边距,使单元格完全充满iText 7,itext7,Itext7,如何在iText 7中创建一个表格,表格中的单元格内部没有任何间距、填充或边距。当我在单元格中放置图像时,它需要将单元格填充到100%,触摸边框时没有任何空间 我尝试将此添加到我的手机: Cell qrImageCell = new Cell(); qrImageCell.setMargin(0f); qrImageCell.setPadding(0f); qrImageCell.setHeight(44f); qrImageCell.add(barcodeImage.setMargins(0f

如何在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);
但无论我尝试什么,二维码和边框之间总是有一个白色的矩形区域(仅供参考:一旦它起作用,我当然会删除边框)

使用iText 7.1.15进行测试 使用此图像:

该代码:

BarcodeQRCode qrCode = new BarcodeQRCode(text);
PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
Image barcodeImage = new Image(barcodeObject).setPadding(0f).scaleAbsolute(44f, 44f)
Table Table=新表(1);
Image barcodeImage=新图像(ImageDataFactory.create(“Image.png”);
单元格qrImageCell=新单元格();
qrImageCell.setBorder(新的SolidBorder(ColorConstants.RED,2));
设置填充(0f);
添加(条形码图像);
表2.addCell(qrImageCell);
单据新增(表);
结果是:

潜在原因 不正确的高度 您的代码中有
qrImageCell.setHeight(44f)
。可能该高度与图像的高度不正确对应。这不太可能是问题所在,因为它不会增加单元格的宽度

其他细胞的影响 将测试表置于两列表之上并添加几个单元格表明,同一行和同一列中单元格的大小将对单元格的大小产生影响

table.addCell(新单元格().add(新段落(“单元格(1,2)”)).setHeight(300));
表.addCell(新单元格().add(新段落(“单元格(2,1)”)).setWidth(300));
表.addCell(新单元格().add(新段落(“单元格(2,2)”));

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

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


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

最后,背景色技巧对我帮助很大。最后,出于某种原因,同样由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”会删除单元格中的任何填充。只需看看是否有可能删除二维码周围的边框