English 中文(简体)
Can you set a background imaged to a PdfPTable when using iText
原标题:
  • 时间:2009-11-09 08:02:22
  •  标签:
  • java
  • itext

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

问题回答

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 s technique has a flaw or two.

  1. A copy of the image is added to the PDF for each cell. Store the returned Image between cellLayout calls so you ll have just one copy. If you have a 10x10 table and a 10kb image, you re taking up 1mb instead of 10kb in the PDF. Ouch. And it s actually a little worse than that with the extra overhead of all those extra objects (not a lot worse, but measurable).
  2. It must tile the image, one per cell.

You re better off going with a PdfPTableEvent. Note that if your table spans multiple pages, your event handler will be called once for each table. The heights and widths parameters are a bit funky. The first value in each is the absolute start position. The remaining values really are heights and widths. Handy, but the variable names are a tad misleading.

And keep in mind that each instance of an image means another copy of that image in your PDF. Save em and reuse em whenever you can.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签