JavaFX convert WritableImage to Image

Viewed 14159

I am doing some application which makes a screenshot of LineChart and save it to pdf file. So I don't know a smooth way to convert WritableImage (JavaFX 2.2) to Image (iText lib).

My temporary solution is:

  1. to make a snapshot, then
  2. get the WritableImage from the snapshot
  3. write the image to a png file
  4. open the image and make iText object Image

I would like to make some changes: I don't want to write the png file to disc, I just want the snapshot to be written to a pdf file.

My temporary solution is:

 WritableImage wim = new WritableImage((int) lineChart.getWidth(),(int) lineChart.getHeight());
 Scene scena = primaryStage.getScene();
 scena.snapshot(wim);
 
 File fileA = new File("C://Graphs/chart.png");
 try {
      ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", fileA);
 }
 catch (Exception s) {
 }
 pdfDocument.add(preface3);
 com.itextpdf.text.Image graph =com.itextpdf.text.Image.getInstance("C://Graphs/chart.png");
 pdfDocument.add((com.itextpdf.text.Element) graph);        
2 Answers
Related