View Full Version : saving ImageElement in PDF

08-01-2002, 09:19 AM
I tried the following code to instanciate an ImageElement. This gives me errors while I try to save the output in PDF. However, there is no error when I preview it.

JFileChooser jfc = new JFileChooser();
java.io.File imgfile = jfc.getSelectedFile();

if (imgfile != null) {
try {
imageElement = ItemFactory.createImageElement(
new Rectangle2D.Float(0,0,0,0),
catch (java.io.IOException e){

The stack trace is given below

ERROR: Failed to draw band

com.jrefinery.report.targets.OutputTargetException: DocumentException

at com.jrefinery.report.targets.PDFOutputTarget.drawImage(PDFOutputTarget.java:715)

at com.jrefinery.report.ImageElement.draw(ImageElement.java:60)

at com.jrefinery.report.Band.draw(Band.java:368)

at com.jrefinery.report.ReportProcessor.draw(ReportProcessor.java:94)

at com.jrefinery.report.ReportProcessor.draw(ReportProcessor.java:68)

at com.jrefinery.report.ReportProcessor.printReportHeader(ReportProcessor.java:116)

at com.jrefinery.report.ReportState$PreReportHeader.advance(ReportState.java:147)

at com.jrefinery.report.JFreeReport.processPage(JFreeReport.java:665)

at com.jrefinery.report.JFreeReport.processReport(JFreeReport.java:551)

at com.jrefinery.report.preview.PreviewFrame.attemptSaveAs(PreviewFrame.java:552)

at com.jrefinery.report.preview.PreviewFrame$DefaultSaveAsAction.run(PreviewFrame.java:142)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:154)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:337)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

08-08-2002, 08:47 AM

sorry for being late. A question: What image did you preview? (Gif, JPeg etc?). IText has some limitations which image-types are supported, so this could be a reason for the error.

I changed the OutputTargetExeption so that the parentException (the orginal DocumentException) is also printed. Can you please check out the last changes and repeat the procedure. I need the filetype you want to image and the (now) complete stacktrace of the parentException.

Thanks in advance and
Have more fun,
said Thomas

08-12-2002, 12:28 AM
Thanks a lot for your reply, though I thought you might not respond to this post. I simply tried to preview a GIF image. BTW, I have solved this problem by modifying the following function

private Image getImage (ImageReference imageRef) throws DocumentException, IOException
if (imageRef.getSourceURL () != null)
return Image.getInstance (imageRef.getSourceURL ());

if (imageRef.getImage () != null)
PngEncoder encoder = new PngEncoder (imageRef.getImage ());
byte[] data = encoder.pngEncode ();
return Image.getInstance (data);

throw new DocumentException ("Neither an URL nor an Image was given to paint the graphics");

I figured out, this problem was due to a bug in iText library. iText is unable to recognize some gif images if it cannot find the header info it looks for. However, java getImage() function is perfectly capable of loading any GIF image.

So, I changed the following line

if (imageRef.getSourceURL () != null)
return Image.getInstance (imageRef.getSourceURL ());

into this

if (imageRef.getSourceURL () != null)
return Image.getInstance(Toolkit.getDefaultToolkit().createImage(imageRef.getSourceURL ()), null);

and Now it's working fine :)

08-12-2002, 03:45 PM

I reworked your changes a little bit and added them to the cvs. Now images are only recoded if the datatype was not recognized. Thanks for this hint and your provided fix, using this fixes also the depency on iTexts Image-Features.

Now JFreeReport supports all image-types the JDK supports, using iTexts features where possible and falling back to the JDK-Features if iTexts fails.

Have more fun,
said Thomas