By Rakesh Menon on Jan 14, 2010
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="http://dl.javafx.com/1.2/dtfx.js"></script> <script src="http://javafxdemos.googlecode.com/hg/resource/JavaFXPrint/JavaFXPrint.js"></script>
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 PrintUtils.java for more information.
Try it out and let me know feedback.. ..and once again wish you a
Happy New Year!