Get rid of blurred chart when saving it to a pdf

人盡茶涼 提交于 2019-12-11 02:48:05

问题


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": If I zoom out to 75% percent or zoom in to 125% the diagramm looks blurry:

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)?


回答1:


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);


来源:https://stackoverflow.com/questions/52067196/get-rid-of-blurred-chart-when-saving-it-to-a-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!