将其保存为pdf时,摆脱模糊图表 [英] Get rid of blurred chart when saving it to a pdf

查看:542
本文介绍了将其保存为pdf时,摆脱模糊图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在互联网上找到了一个如何将javafx图表保存为pdf的例子,所以我试了一下:

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

现在我遇到的问题是当我将javafx区域图表保存到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.

例如:

如果我100%缩放到pdf图表消失:

如果我缩小到75%或放大到125%,则图形看起来很模糊:

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:



现在我的问题是,如果有一些方法可以在pdf中制作图表看起来更清晰,因为在程序中图表看起来正常(不模糊,看起来很清晰)?

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

推荐答案

通过应用创建一个更大的图像JavaFX中的比例转换:

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

稍后创建PDF反向比例以使其显得更小:

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

contentStream.drawImage(pdImageXObject, 150, 500, pdImageXObject.getWidth() / 5, pdImageXObject.getHeight() / 5);

这篇关于将其保存为pdf时,摆脱模糊图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆