tags:

views:

382

answers:

2

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

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

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

Thanks

+1  A: 

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

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();
        }
    }

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

Prabhat