我正在使用 iText 库创建 PDF,但无法设置 PDF 中表格的背景图像。
如下图
编辑
目前我正在使用它来设置背景,其设置图像为绝对位置,我想将其设置为相对于表格
class CellBackgroundPic implements PdfPTableEvent {
Activity mActivity;
public CellBackgroundPic (Activity Activity){
this.mActivity=Activity;
}
Image bgImage;
public void tableLayout(PdfPTable table, float[][] widths, float[] heights,
int headerRows, int rowStart, PdfContentByte[] canvases){
PdfContentByte pdfContentByte = canvases[PdfPTable.BACKGROUNDCANVAS];
Drawable myImage = mActivity.getResources().getDrawable(R.drawable.table_bg);
Bitmap bitmap = ((BitmapDrawable) myImage).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
try {
bgImage = Image.getInstance(bitmapdata);
bgImage.setAbsolutePosition(330f, 642f);
pdfContentByte.addImage(bgImage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最佳答案
可能有不同的方法。
一种方法是使用通过图像创建的图案颜色,并将该图案颜色用作背景颜色。但是,这似乎与您的需要不匹配。
根据您的示例,我建议使用表事件,如下所示: http://itextpdf.com/examples/iia.php?id=93
在此示例中,行的背景色是在 tableLayout()
方法中绘制的。您需要调整此方法,而不是绘制彩色矩形,您需要在适当的坐标处添加图像。
关于java - 如何使用 iText 库在 PDF 表格上应用背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452479/