Java Printing bug on small images

We get to find some intriguing bugs in Java, and today I stumbled across an interesting print issue. This exception was being thrown in Java Printing

java.lang.IllegalArgumentException: Width (3) and height (0) must be > 0
at java.awt.image.SampleModel.<init>(SampleModel.java:108)
at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:128)
at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:69)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:638)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:265)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:368)
at sun.print.PSPathGraphics.redrawRegion(PSPathGraphics.java:626)
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1974)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1435)
at sun.print.UnixPrintJob.pageableJob(UnixPrintJob.java:643)
at sun.print.UnixPrintJob.print(UnixPrintJob.java:459)

As the exception was happening deep inside the JDK and not our code it was a rather difficult issue to hunt down. We had to step through the PDF commands until we identified the one causing the issue.

It turned out that Java printing was being passed a tiny (8×1 pixels) image. This was being scaled down with an AffineTransform to produce a final image with a height less than 1. As Java cannot handle fractions of pixels, this was being truncated to zero and causing the issue. Adding some code to ignore the image removed the issue.
This post is part of our “Printing Articles Index” in these articles, we aim to help you understand printing in Java and PDF’s.

Related Posts:

  • No Related Posts
The following two tabs change content below.

Mark Stephens

System Architect and Lead Developer at IDRSolutions
Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.
Markee174

About Mark Stephens

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>