Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Java Printing bug on small images

52 sec read

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.

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

Leave a Reply

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

IDRsolutions Ltd 2019. All rights reserved.