有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

itext pdf文档中的java JavaFX图形

我面临着以下任务:

我得到了一些数据(不管如何),必须生成pdf格式的报告。我的程序通常有一个用JavaFX制作的GUI,但是pdf部分在一个单独的线程中运行。一般来说,创建pdf是没有问题的。我正在使用iText作为pdf库

问题是,我必须在pdf文档中包含一个非常复杂的图形。图形本身也没有问题,因为我非常熟悉JavaFX的图表

目前,我正计划像这样解决图表问题:

// at first create the chart
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
final LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);

// format the graph and add the data
...not shwon here, not important...

// now get the chart as picture to embed in the document
WritableImage chartWrtImg                   = chart.snapshot(new SnapshotParameters(), null);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

try {

    // convert the WritableImage from javaFX to an Image from itext
    ImageIO.write(SwingFXUtils.fromFXImage(chartWrtImg, null), "png", byteArrayOutputStream);
    Image graphImg = Image.getInstance(byteArrayOutputStream.toByteArray());

    // scale and position the picture
    graphImg.setAbsolutePosition(35, docHeight - 460);
    graphImg.scaleAbsolute(docWidth - 80, 350);

    // the itext pdf document
    document.add(graphImg);

} catch (IOException e) {
    e.printStackTrace();
}

目前,我在运行pdf创建时遇到以下异常:

java.lang.ClassNotFoundException: javafx.embed.swing.SwingFXUtils

为什么我会得到显示的异常?我正在通过maven导入JavaFXVersion13。lib应该在那里(并且正在应用程序线程上工作)

一般来说,使用这样的JavaFX图表为pdf文档创建一个图形是明智的做法吗


共 (0) 个答案