所以我在互联网上找到了一个如何将javafx图表保存为pdf的示例,所以我尝试了一下:
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时,图表看起来有点模糊。
例如:
如果我将PDF缩放100%,则图表“消失”: 如果我缩小到75%或放大到125%,图表将变得模糊:
现在我的问题是,是否可以通过某种方法使pdf中的图表看起来更清晰,因为在程序中该图表看起来很正常(不模糊,看起来很清晰)?
答案 0 :(得分:2)
通过在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反向比例以使其显得更小时
contentStream.drawImage(pdImageXObject, 150, 500, pdImageXObject.getWidth() / 5, pdImageXObject.getHeight() / 5);