2

So I found an example on the internet how to save a javafx chart to a pdf, so I tried it out:

final AreaChart<Number, Number> arechart = new AreaChart<>(new NumberAxis(0, 3, 0.5), new NumberAxis(0, 3, 0.5));
xAxis.setLabel("average quality");
yAxis.setLabel("average quantity");
sc.setTitle("Producerdata");

XYChart.Series series1 = new XYChart.Series();
series1.setName("Water 11");

producer.getProducts().forEach((pr) -> {
    if (pr.getName().equals("Water 11")) {
        series1.getData().add(new XYChart.Data(pr.getPercentQual(), pr.getAmount()));
    }
});

XYChart.Series series2 = new XYChart.Series();
series2.setName("Water E40");
producer.getProducts().forEach((pr) -> {
    if (pr.getName().equals("Water E40")) {
        series2.getData().add(new XYChart.Data(pr.getPercentQual(), pr.getAmount()));
    }
});
arechart.getData().addAll(series1, series2);
PDDocument newPDF=new PDDocument();
PDPage chartPage = new PDPage();
newPDF.addPage(chartPage);
WritableImage image = arechart.snapshot(new SnapshotParameters(), null);
BufferedImage bf= SwingFXUtils.fromFXImage(image, null);
PDImageXObject pdImageXObject = LosslessFactory.createFromImage(newPDF, bf);
PDPageContentStream contentStream = new PDPageContentStream(newPDF, chartPage);
contentStream.drawImage(pdImageXObject, 150, 500, pdImageXObject.getWidth()  , pdImageXObject.getHeight() );
contentStream.close();
newPDF.close();
newPDF.save(new File("C:\\Users\\chelsfan\\Desktop\\TestingNetbeans\\PDFS\\chart.pdf"));

Now the problem I am struggling with is that when I save the javafx area diagramm to the pdf, the chart looks kinda blurry.

For e.g:

If I zoom 100% to the pdf the diagramm "dissapears": enter image description here If I zoom out to 75% percent or zoom in to 125% the diagramm looks blurry:

enter image description here enter image description here Now my question is, if there is some way to make the chart in the pdf looking sharper, because in the programm the chart looks normal (not blurry,looks sharp)?

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Lebron11
  • 656
  • 7
  • 18
  • You didn't put the full code... most likely, the answer is to make the original chart bigger and then to scale more. The perfect solution would be to use vector graphics, which would of course be much more work. – Tilman Hausherr Aug 29 '18 at 04:24
  • @TilmanHausherr I added the arechart code above. Where can I set the size/scale? – Lebron11 Aug 29 '18 at 08:20
  • The simplest way is to divide `pdImageXObject.getWidth() , pdImageXObject.getHeight()` by a number. – Tilman Hausherr Aug 29 '18 at 08:22
  • Re the size of the original image, I don't know how to increase that one. I thought this was related to the scene but it is not. Maybe some parameter in `snapshot`. – Tilman Hausherr Aug 29 '18 at 08:32
  • @TilmanHausherr hmm.maybe . when I dividide the width and length of the imageobject and zoom in to 100% the chart does not dissapear but still looks kinda blury – Lebron11 Aug 29 '18 at 08:48
  • To be sure that we talk about the same thing: the key is to create a much larger BufferedImage. Please save that one with `ImageIO.write()` and look whether it has the same quality, i.e. is not blurry. I don't know anything about JavaFX. Maybe this is done by using SnapshotParameters (I see it is possble to provide a transform), maybe this is done by providing a huge image as parameter to `sc.snapshot`. – Tilman Hausherr Aug 29 '18 at 09:06
  • @TilmanHausherr I saved the chart as a Image. The image is not blurry (100%) only when I zoom In on the image the image gets blurry. But normally it is not blurry – Lebron11 Aug 29 '18 at 09:13
  • What I mean is this: lets say your saved chart is 500 x 500. When you zoom that it will always look blurry. It's not like in the movie "clear and present danger" where Harrison Ford asks for "more details" and now you see the face of the terrorist in a previously blurry image. You need to create a larger image out of JavaFX, e.g. 2500 x 2500 or whatever. – Tilman Hausherr Aug 29 '18 at 09:56

1 Answers1

2

Create a larger image by applying a scale transform in JavaFX:

SnapshotParameters sp = new SnapshotParameters();
Transform transform = Transform.scale(5, 5); // increase for larger image
sp.setTransform(transform);
WritableImage image = arechart.snapshot(sp, null);
BufferedImage bf= SwingFXUtils.fromFXImage(image, null);
ImageIO.write(bf, "png", new File("JavaFXTest.png")); // remove this line in production, this is just for you to see that the image is larger but not blurry

later when creating the PDF reverse scale to make it appear smaller:

contentStream.drawImage(pdImageXObject, 150, 500, pdImageXObject.getWidth() / 5, pdImageXObject.getHeight() / 5);
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • well it works a little bit better. now if I have normal zoom (100%) it looks a little bit blurry but the good part is that if zoom more it the cleaner/less blurry it gets. Which parameter do I have to change so that It looks more and more clearer by default zoom – Lebron11 Aug 29 '18 at 12:00
  • Increase the "5" at 4 places. I can't test myself because your code isn't working. The declarations of xAxis, yAxis, sc, and producer are missing. – Tilman Hausherr Aug 29 '18 at 12:08