【发布时间】:2009-11-09 08:02:22
【问题描述】:
我正在使用 iText 为数据库中的数据生成 Pdf 报告...
pdf 页面的标题是一个图像,图像上动态添加了一些文本,例如生成日期..
任何人都知道我们是否可以在 itext 中将背景图像设置为 PdfPTable 类型的表格..
谢谢
【问题讨论】:
我正在使用 iText 为数据库中的数据生成 Pdf 报告...
pdf 页面的标题是一个图像,图像上动态添加了一些文本,例如生成日期..
任何人都知道我们是否可以在 itext 中将背景图像设置为 PdfPTable 类型的表格..
谢谢
【问题讨论】:
我知道它很晚,但可能会帮助某人。方法如下。
创建一个BGClass类,实现PdfPCellEvent,输入如下方法。
@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 是您想要背景图像的单元格。
【讨论】:
Prabhat 的技术有一两个缺陷。
您最好使用PdfPTableEvent。请注意,如果您的表格跨越多个页面,您的事件处理程序将为每个表格调用一次。 heights 和 widths 参数有点古怪。每个中的第一个值是绝对起始位置。其余的值实际上是高度和宽度。方便,但变量名称有点误导。
请记住,图像的每个实例都意味着 PDF 中该图像的另一个副本。尽可能保存并重复使用它们。
【讨论】: