如何在iText中设置PdfPCell中的背景图像? [英] How to set background image in PdfPCell in iText?

查看:1766
本文介绍了如何在iText中设置PdfPCell中的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用iText生成PDF报告。我想在 PdfPCell 中设置中等大小的图像作为背景,而不是使用背景颜色。这可能吗?

I am currently using iText to generate PDF reports. I want to set a medium size image as a background in PdfPCell instead of using background color. Is this possible?

推荐答案

你可以找到一个关于如何用iText 5.5.1做的例子此处。您需要创建自己的 PdfPCellEvent 接口的实现,例如:

You can find an example on how to do this with iText 5.5.1 here. You need to create your own implementation of the PdfPCellEvent interface, for instance:

class ImageBackgroundEvent implements PdfPCellEvent {

    protected Image image;

    public ImageBackgroundEvent(Image image) {
        this.image = image;
    }

    public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
        try {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            image.scaleAbsolute(position);
            image.setAbsolutePosition(position.getLeft(), position.getBottom());
            cb.addImage(image);
        } catch (DocumentException e) {
            throw new ExceptionConverter(e);
        }
    }

然后你需要创建一个这个事件的实例,将它声明到需要此背景的单元格:

Then you need to create an instance of this event and declare it to the cell that needs this background:

Image image = Image.getInstance(IMG1);
cell.setCellEvent(new ImageBackgroundEvent(image));

此代码已使用最新版本的iText进行测试,结果显示为这个。您在包名称(com.lowagie)中使用了我的名字(Lowagie)的iText版本。这意味着此样本可能有效,也可能无效。我们不知道,我们不会测试您使用的版本多年前已被宣布为EOL。它不再受支持。

This code was tested with the most recent version of iText and the result looks like this. You're using a version of iText with my name (Lowagie) in the package names (com.lowagie). This means that this sample may or may not work. We don't know and we won't test as the version you're using has been declared EOL years ago. It is no longer supported.

这篇关于如何在iText中设置PdfPCell中的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆