使用iText时,是否可以将背景图像设置为PdfPTable [英] Can you set a background imaged to a PdfPTable when using iText

查看:784
本文介绍了使用iText时,是否可以将背景图像设置为PdfPTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText为数据库中的数据生成Pdf报告...

I am using iText to generate Pdf Reports for data in a database...

pdfs页面的标题是一个带有一些文本的图像动态添加的图像,例如生成的日期..

The header of the pages of the pdfs is an image with some text on the image added dynamically, say for example date generated..

任何人都知道我们是否可以在itext中将背景图像设置为PdfPTable类型的表..

Anyone knows if we can set background images to Tables of type PdfPTable in itext..

谢谢

推荐答案

我知道它很晚但可能对某人有帮助。这是实现它的方法。

I know its very late but might help someone. Here is the way to do it.

创建一个类BGClass,实现PdfPCellEvent并输入以下方法。

Create a class BGClass, implement PdfPCellEvent and enter following method.

@Override
    public void cellLayout(PdfPCell arg0, Rectangle arg1, PdfContentByte[] arg2) {
        try {
            PdfContentByte pdfContentByte = arg2[PdfPTable.BACKGROUNDCANVAS];
            Image bgImage = Image.getInstance("URL_TO_YOUR_IMAGE");
            pdfContentByte.addImage(bgImage, arg1.getWidth(), 0, 0, arg1
                    .getHeight(), arg1.getLeft(), arg1.getBottom());

        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在主要课程中,您要创建PDF, pdfpCell.setCellEvent(new BGClass());其中pdfpCell是您想要背景图像的单元格。

In your main class, where you are creating PDF, pdfpCell.setCellEvent(new BGClass()); where pdfpCell is the cell for which you want background image.

这篇关于使用iText时,是否可以将背景图像设置为PdfPTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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