【问题标题】:Set the text above the image in pdf cell using itext使用 itext 在 pdf 单元格中设置图像上方的文本
【发布时间】:2014-05-02 09:18:33
【问题描述】:

我使用 iText 创建了 pdf。在那,如何将背景图像设置为特定的 PdfCell 并将文本放在同一个单元格中..

我通过以下方式将背景图像设置为该单元格:

    cell.addElement(image_green_left);

如果是文字和图片,则只显示图片,文字被隐藏。

    cell.addElement(image_green_left);
    cell.addElement(new Paragraph("Test 3"));
    table.addCell(cell);

如何同时显示文字和图片?

【问题讨论】:

    标签: android pdf itext


    【解决方案1】:

    创建您自己的 PdfPCellEvent 实现,例如:

    class ImageBackground implements PdfPCellEvent {
        protected Image image;
        public ImageBackground(Image image) {
            this.image = image;
        }
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            Image.scaleToFit(position.getWidth() , position.getHeight());
            Image.setAbsolutePosition(position.getBottom(), position.getLeft());
            cb.addImage(image);
        }
    }
    

    现在当cell 是带有文本的PdfPCellbgImage 是图像的路径时,请执行以下操作:

    Image image = Image.getInstance(bgImage);
    cell.setCellEvent(new ImageBackground(image));
    

    现在,您将拥有一个充当单元格背景的图像。

    【讨论】:

    • 对于已经使用PDFPCell显示的图像,是否可以setBackgroundColor
    猜你喜欢
    • 2021-11-09
    • 1970-01-01
    • 2014-05-12
    • 2017-02-19
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多