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.