I am creating a PDF using the iText Library and not been able to set the background image for Table in PDF.
like below image
Edit
Currently i am using this to set Background , its setting image to absolute position , i want to set it relative to table
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();
}
}
}
There are different approaches possible.
One would be to use a pattern color created with an image and use that pattern color as background color. However, that doesn’t seem to be a match with what you need.
Based on your example I would suggest using a table event as demonstrated here:
http://itextpdf.com/examples/iia.php?id=93
In this example, the background color of the rows is drawn in the
tableLayout()method. You need to adapt this method and instead of drawing colored rectangles, you need to add an image at the appropriate coordinates.