Transparent Images in exportToPDF()

View: New views
2 Messages — Rating Filter:   Alert me  

Transparent Images in exportToPDF()

by Ryan Andreasen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a report that has multiple images in it.  Some of these images have an image expression of java.net.URL and others of java.awt.Image.  (The java.awt.Image files are not stored on the disk but in memory when the report is generated.)  All of the images that are put in the report have a transparent background.

After creating a JasperPrint for the report I am able to view the report using JasperViewer and the images in the report look fine.  I am also able to use the JasperPrintManager and the report looks fine (even when printing to PDFCreator).  However, when I use the JasperExportManager.exportReportToPdf() method, the images' transparencies do not work (they are blackish/redish and the image is indistinguishable).  I am exporting the report to a pdf, getting the byte[] and saving it to a file.  The BufferedImages I give the report are of type BufferedImage.TYPE_INT_ARGB.  Is there a bug with the exportReportToPdf() that does not handle image transparencies--or is this expected behavior?  It seems odd that the JasperViewer could handle transparent images but not the JasperExportManager.

I am using the jasperreports-2.0.4.jar and the iText-2.0.7.jar files.  I am really stuck on this and thank anyone in advance for further explanation and help.

Thanks!!

Re: Transparent Images in exportToPDF()

by kwutchak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Flaxxed wrote:
I have a report that has multiple images in it. [snip] All of the images that are put in the report have a transparent background. [snip] However, when I use the JasperExportManager.exportReportToPdf() method, the images' transparencies do not work [snip]
I ran into the same problem, but wasn't able to find a solution on the web.  I contacted Flaxxed, who posed the question above, and luckily he had found a solution.

The gist of the solution is to pass a JRRenderable object to Jasper and not a java.awt.Image.  I found that in addition I needed to ensure that my Image files were sent to Jasper as a png with an alpha channel.  Below is the code that I ended up with.

            imageOriginal = ... whatever your image source is...

            // These steps are necessary to ensure that transparency works
            int height = imageOriginal.getHeight( null );
            int width  = imageOriginal.getWidth( null );

            bufferedImage = new BufferedImage( width, height, BufferedImage.TYPE_4BYTE_ABGR );
            Graphics2D graphics = bufferedImage.createGraphics();
            graphics.drawImage( imageOriginal, 0, 0, null );
            graphics.dispose();

            JRRenderable jrRenderable =
                JRImageRenderer.getInstance( image, JRRenderable.IMAGE_TYPE_PNG, JRImage.ON_ERROR_TYPE_ERROR );

            return jrRenderable;

JRImageRenderer.getInstance did not work properly until I forced it to PNG.

I hope this helps.