Thursday Jan 14, 2010

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="http://dl.javafx.com/1.2/dtfx.js"></script> <script src="http://javafxdemos.googlecode.com/hg/resource/JavaFXPrint/JavaFXPrint.js"></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 PrintUtils.java 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="http://javafxdemos.googlecode.com/hg/resource/JavaFXPrint/zoneit.js"></script>

About


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

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today