JavaFX - Printing

There were many queries related to saving and printing of JavaFX nodes. Users can capture the node as image following the approach outlined in one of previous blog and use ImageIO APIs to save the image to file. For printing, we can use 2D printing - java.awt.print.PrinterJob and java.awt.print.Printable. Implement print method in Printable interface and draw the image onto Graphics object. Another approach is to directly print the image using javax.print.PrintService.

Below sample demonstrates use of ImageIO and PrintService to print the Chart

<script src=""></script> <script src=""></script>

To launch click on above image or

The chart node is captured in BufferedImage. Refer to toBufferedImage method in Utils.fx. The image is then streamed to a ByteArrayOutputStream using ImageIO API. A PrintService instance which supports DocFlavor.INPUT_STREAM.PNG is obtained using lookupPrintServices method in PrintServicesLookup and print. Refer to print method in for more information.

Try it out and let me know feedback.. ..and once again wish you a
Happy New Year!

<script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src=""></script>


The print button simply freezes the applet on my system, or does it? I came back to the page to give a second shot. I hit print, it froze. I left the page open while viewing javalobby. Two minutes later, the print dialog popped up.

Posted by Jonathan on January 15, 2010 at 10:53 AM IST #

@Jonathan Thanks for feedback.. Can you provide some more details - OS, JDK-Version, Printer-Info, Local/Network Printer. Yes, there is a chance for freeze if lookup takes longer, so would to find the exact cause..

Posted by Rakesh Menon on January 15, 2010 at 01:20 PM IST #

windows vista 32bit
quad-core q6600 cpu, 4gb memory
java 6 32bit (not sure what update - im at work now)
some hp printer (not connected)
cutepdf writer (print to pdf).

I usually use cutepdf or acrobat to test printing features so I don't waste paper.

On my work machine, there is only a delay of ~1-2 seconds the initial time. But I'm also using a super computer with 12GB of memory so I hope it would be fairly fast.

Posted by Jonathan on January 15, 2010 at 03:08 PM IST #

I have a virtual PDF printer as the default printer, but the result is an XPS file "printed" (generated) by another virtual printer (XPS), which is the FIRST printer in the printers' dropdown list. The same result happens if I choose a different printer (real or virtual).

I have:
- windows vista 32bit
- java 6 u18
- ms xps writer (to xps)
- ms livemeeting writer
- hp officejet local printer (disconnected)
- hp deskjet local printer (connected)
- doPdf v6 (to pdf) <default>

In addition, the Properties button in the Printer dialog box appear disabled, so I can't set the printer's options.

Posted by Willy on January 19, 2010 at 10:34 PM IST #

@Jonathan I will try with cutepdf writer, most likely its not supporting the direct print of image stream and must be throwing exception, which is not handled properly in code.

Posted by Rakesh Menon on January 21, 2010 at 01:40 AM IST #

@Willy Its again due to hardcoding in code, sorry I didn't focus much on the actual printing code. I will resolve them..

-- From --

DocPrintJob job = printServices[0].createPrintJob();

I'm always creating print-job from first supported PrintService. So it ignores user's selection.

Posted by Rakesh Menon on January 21, 2010 at 01:43 AM IST #

Hi Rakesh!

Will this printing app work in Windows CE (Windows Mobile 6) with JavaFX installed? It has a bluetooth printer paired.

Thanks in advance!

Posted by Sankha on February 26, 2010 at 05:29 AM IST #

I think the "java.awt" package is missing from the Windows Mobile version of JavaFX.

Posted by Sankha on February 26, 2010 at 05:31 AM IST #

In fact there's no "java.awt" package in JavaFX at all.

Posted by Sankha on February 26, 2010 at 06:09 AM IST #

@Sankha This post is specific to Desktop profile. May be I must start tagging my post as Common/Desktop. I haven't tried anything specific to mobile, may be you can check mobile forum to find solution.

Posted by Rakesh Menon on February 26, 2010 at 06:09 AM IST #

Yes it will be useful if you specify the platform since it's in Java which is so platform dependent although some people say otherwise.

ex: PC/Smart Phones etc.

or OS-wise.

ex: WinXp / Ubuntu / WinCE etc.

Posted by Sankha on February 26, 2010 at 09:39 AM IST #

Hi Rakesh!
Button "Save" save file with no extension. Then how can open is unclear? But button 'print' instead of printing will save with xps extension, and can open only with IE. But printing?
Thanks in advance!

Posted by Alex on February 28, 2010 at 12:36 AM IST #

@Alex Image is saved in PNG format, you can update the code to add ".png", suffix if it doesn't exist in file name..

The format in which the image is printed depends on the PrintService. As of now I hardcoded to use the first PrintService (index 0 is hardcoded), instead we can provide option for user to select it. This is more of JavaSE APIs, so I didn't focus on it. You can refer to PrintService help. If there are still issues, I'll update sample..

Posted by Rakesh Menon on March 01, 2010 at 02:59 AM IST #

Hi Rakesh!
Printing does work in my PC depends choice Print Service for other applications, and is not for this example. When choosing to file, print to file. By choosing to printer print to file too and never to printer.
Thanks in advance!

Posted by Alex on March 10, 2010 at 01:48 PM IST #

@Alex Yes, many people reported same issue, so I must fix my code to provide option to pick up PrintService, as of now I pick the first PrintService (which many not be a real Printer). I didn't focus on it, because that code is not specific to FX. But yes, I must fix this.

Posted by Rakesh Menon on March 11, 2010 at 01:06 AM IST #

Post a Comment:
Comments are closed for this entry.

The views expressed on this blog are my own and do not necessarily reflect that of my organization


« July 2016