【问题标题】:Remove any spacing, padding, and margin so a cell is filled completely with iText 7删除任何间距、填充和边距,以便单元格完全用 iText 7 填充
【发布时间】:2021-04-22 18:03:34
【问题描述】:

如何在 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);

但无论我尝试什么,QR 码和边框之间总是有一个白色矩形区域(仅供参考:一旦边框起作用,我当然会删除它。

【问题讨论】:

    标签: itext7


    【解决方案1】:

    使用 iText 7.1.15 进行测试

    有了这张图片:

    还有这段代码:

    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)。也许该高度与图像的高度不正确对应。虽然这不太可能是问题所在,因为它不会增加单元格的宽度。

    其他细胞的影响

    将测试的表格制作成2列的表格上方并添加几个单元格,可以看出同一行同列的单元格大小会对单元格的大小产生影响。

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

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

    没有空格

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

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

    【讨论】:

      【解决方案2】:

      最后,背景色技巧帮了我很多。最后,由于某种原因也由 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 码周围的边框。

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 2020-03-04
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        相关资源
        最近更新 更多