Hallo, ich versuche gerade einen PDF-Vorschau mit ICEPDF, JavaFX und ANT zu bauen dazu verwende ich folgenden Code:
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.util.FontPropertiesManager;
import org.icepdf.ri.util.ViewerPropertiesManager;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
/**
* Example of integrating ICEpdf into a JavaFX application using Java 8 and SwingNode.
*/
public class PDFFXMLController extends Application {
private static String pdfPath;
private SwingController swingController;
private JComponent viewerPanel;
public static void main(String[] args) {
// pdfPath = args[0];
pdfPath = "C:\\pdf-dokument.pdf"; // <-------------- Hier ist die einzige Modifikation!
launch(args);
}
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
Scene scene = new Scene(borderPane);
// add viewer content pane
createViewer(borderPane);
borderPane.setPrefSize(1025, 600);
createResizeListeners(scene, viewerPanel);
primaryStage.setOnCloseRequest(we -> SwingUtilities.invokeLater(() -> swingController.dispose()));
primaryStage.setTitle("JavaFX PDF Viewer Demo");
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
primaryStage.show();
openDocument(pdfPath);
}
private void createResizeListeners(Scene scene, JComponent viewerPanel) {
scene.widthProperty().addListener((observable, oldValue, newValue) -> SwingUtilities.invokeLater(() -> {
viewerPanel.setSize(new Dimension(newValue.intValue(), (int) scene.getHeight()));
viewerPanel.setPreferredSize(new Dimension(newValue.intValue(), (int) scene.getHeight()));
viewerPanel.repaint();
}));
scene.heightProperty().addListener((observable, oldValue, newValue) -> SwingUtilities.invokeLater(() -> {
viewerPanel.setSize(new Dimension((int) scene.getWidth(), newValue.intValue()));
viewerPanel.setPreferredSize(new Dimension((int) scene.getWidth(), newValue.intValue()));
viewerPanel.repaint();
}));
}
private void createViewer(BorderPane borderPane) {
try {
SwingUtilities.invokeAndWait(() -> {
// create the viewer ri components.
swingController = new SwingController();
swingController.setIsEmbeddedComponent(true);
// read/store the font cache.
FontPropertiesManager.getInstance().loadOrReadSystemFonts();
ViewerPropertiesManager properties = ViewerPropertiesManager.getInstance();
properties.getPreferences().putFloat(ViewerPropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, 1.25f);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_OPEN, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_SAVE, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_PRINT, false);
// hide the status bar
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_STATUSBAR, false);
// hide a few toolbars, just to show how the prefered size of the viewer changes.
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_FIT, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ROTATE, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_FORMS, false);
swingController.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(swingController.getDocumentViewController()));
SwingViewBuilder factory = new SwingViewBuilder(swingController, properties);
viewerPanel = factory.buildViewerPanel();
viewerPanel.revalidate();
SwingNode swingNode = new SwingNode();
swingNode.setContent(viewerPanel);
borderPane.setCenter(swingNode);
/*
// add toolbar to the top.
FlowPane toolBarFlow = new FlowPane();
JToolBar mainToolbar = factory.buildCompleteToolBar(true);
buildJToolBar(toolBarFlow, mainToolbar);
borderPane.setTop(toolBarFlow);
// main utility pane and viewer
SwingNode swingNode = new SwingNode();
viewerPanel = factory.buildUtilityAndDocumentSplitPane(true);
swingNode.setContent(viewerPanel);
borderPane.setCenter(swingNode);
// the page view menubar
FlowPane statusBarFlow = new FlowPane();
buildButton(statusBarFlow, factory.buildPageViewSinglePageNonConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewSinglePageConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewFacingPageNonConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewFacingPageConToggleButton());
borderPane.setBottom(statusBarFlow);
*/
});
} catch (InterruptedException | InvocationTargetException e) {
e.printStackTrace();
}
}
private void openDocument(String document) {
SwingUtilities.invokeLater(() -> {
swingController.openDocument(document);
viewerPanel.revalidate();
});
}
private void buildButton(FlowPane flowPane, AbstractButton jButton){
SwingNode swingNode = new SwingNode();
swingNode.setContent(jButton);
flowPane.getChildren().add(swingNode);
}
private void buildJToolBar(FlowPane flowPane, JToolBar jToolBar){
SwingNode swingNode = new SwingNode();
swingNode.setContent(jToolBar);
flowPane.getChildren().add(swingNode);
}
}
Folgende Libraries sind integriert:
icepdf-code-7.2.1.jar
icepdf-viewer-7.2.1.jar
pdfbox-app-3.0.3.jar
JavaFX 23
RUN VM-Option: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml
Ich bekomme folgende Nachricht:
run:
java.lang.reflect.InvocationTargetException
at java.desktop/java.awt.EventQueue.invokeAndWait(EventQueue.java:1371)
at java.desktop/java.awt.EventQueue.invokeAndWait(EventQueue.java:1346)
at java.desktop/javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1480)
at pdffxmlcontroller.PDFFXMLController.createViewer(PDFFXMLController.java:237)
at pdffxmlcontroller.PDFFXMLController.start(PDFFXMLController.java:204)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@23.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@23.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@23.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.embed.swing.SwingNodeHelper (in unnamed module @0x3a45f731) cannot access class com.sun.javafx.scene.NodeHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene to unnamed module @0x3a45f731
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1027)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at javafx.embed.swing.SwingNode.<clinit>(SwingNode.java:136)
at pdffxmlcontroller.PDFFXMLController.lambda$createViewer$6(PDFFXMLController.java:267)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:308)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Nov. 04, 2024 9:08:09 AM org.icepdf.core.pobjects.Catalog <clinit>
INFORMATION: ICEpdf Core 7.2.1
BUILD SUCCESSFUL (total time: 5 seconds)
Wo ist das Problem hat jemand eine Idee?
Ich verwende Java 21 Swing Variante funktioniert ohne Probleme. Ich möchte aber JavaFX haben.
icepdf/examples/javafx/src/main/java/org/icepdf/examples/javafx/PdfFXViewer.java at main · pcorless/icepdf
PDF Rendering and Viewing API in Java. Contribute to pcorless/icepdf development by creating an account on GitHub.
github.com
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import org.icepdf.ri.util.FontPropertiesManager;
import org.icepdf.ri.util.ViewerPropertiesManager;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
/**
* Example of integrating ICEpdf into a JavaFX application using Java 8 and SwingNode.
*/
public class PDFFXMLController extends Application {
private static String pdfPath;
private SwingController swingController;
private JComponent viewerPanel;
public static void main(String[] args) {
// pdfPath = args[0];
pdfPath = "C:\\pdf-dokument.pdf"; // <-------------- Hier ist die einzige Modifikation!
launch(args);
}
@Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
Scene scene = new Scene(borderPane);
// add viewer content pane
createViewer(borderPane);
borderPane.setPrefSize(1025, 600);
createResizeListeners(scene, viewerPanel);
primaryStage.setOnCloseRequest(we -> SwingUtilities.invokeLater(() -> swingController.dispose()));
primaryStage.setTitle("JavaFX PDF Viewer Demo");
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.centerOnScreen();
primaryStage.show();
openDocument(pdfPath);
}
private void createResizeListeners(Scene scene, JComponent viewerPanel) {
scene.widthProperty().addListener((observable, oldValue, newValue) -> SwingUtilities.invokeLater(() -> {
viewerPanel.setSize(new Dimension(newValue.intValue(), (int) scene.getHeight()));
viewerPanel.setPreferredSize(new Dimension(newValue.intValue(), (int) scene.getHeight()));
viewerPanel.repaint();
}));
scene.heightProperty().addListener((observable, oldValue, newValue) -> SwingUtilities.invokeLater(() -> {
viewerPanel.setSize(new Dimension((int) scene.getWidth(), newValue.intValue()));
viewerPanel.setPreferredSize(new Dimension((int) scene.getWidth(), newValue.intValue()));
viewerPanel.repaint();
}));
}
private void createViewer(BorderPane borderPane) {
try {
SwingUtilities.invokeAndWait(() -> {
// create the viewer ri components.
swingController = new SwingController();
swingController.setIsEmbeddedComponent(true);
// read/store the font cache.
FontPropertiesManager.getInstance().loadOrReadSystemFonts();
ViewerPropertiesManager properties = ViewerPropertiesManager.getInstance();
properties.getPreferences().putFloat(ViewerPropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, 1.25f);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_OPEN, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_SAVE, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_UTILITY_PRINT, false);
// hide the status bar
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_STATUSBAR, false);
// hide a few toolbars, just to show how the prefered size of the viewer changes.
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_FIT, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_ROTATE, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL, false);
properties.getPreferences().putBoolean(ViewerPropertiesManager.PROPERTY_SHOW_TOOLBAR_FORMS, false);
swingController.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(swingController.getDocumentViewController()));
SwingViewBuilder factory = new SwingViewBuilder(swingController, properties);
viewerPanel = factory.buildViewerPanel();
viewerPanel.revalidate();
SwingNode swingNode = new SwingNode();
swingNode.setContent(viewerPanel);
borderPane.setCenter(swingNode);
/*
// add toolbar to the top.
FlowPane toolBarFlow = new FlowPane();
JToolBar mainToolbar = factory.buildCompleteToolBar(true);
buildJToolBar(toolBarFlow, mainToolbar);
borderPane.setTop(toolBarFlow);
// main utility pane and viewer
SwingNode swingNode = new SwingNode();
viewerPanel = factory.buildUtilityAndDocumentSplitPane(true);
swingNode.setContent(viewerPanel);
borderPane.setCenter(swingNode);
// the page view menubar
FlowPane statusBarFlow = new FlowPane();
buildButton(statusBarFlow, factory.buildPageViewSinglePageNonConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewSinglePageConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewFacingPageNonConToggleButton());
buildButton(statusBarFlow, factory.buildPageViewFacingPageConToggleButton());
borderPane.setBottom(statusBarFlow);
*/
});
} catch (InterruptedException | InvocationTargetException e) {
e.printStackTrace();
}
}
private void openDocument(String document) {
SwingUtilities.invokeLater(() -> {
swingController.openDocument(document);
viewerPanel.revalidate();
});
}
private void buildButton(FlowPane flowPane, AbstractButton jButton){
SwingNode swingNode = new SwingNode();
swingNode.setContent(jButton);
flowPane.getChildren().add(swingNode);
}
private void buildJToolBar(FlowPane flowPane, JToolBar jToolBar){
SwingNode swingNode = new SwingNode();
swingNode.setContent(jToolBar);
flowPane.getChildren().add(swingNode);
}
}
Folgende Libraries sind integriert:
icepdf-code-7.2.1.jar
icepdf-viewer-7.2.1.jar
pdfbox-app-3.0.3.jar
JavaFX 23
RUN VM-Option: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml
Ich bekomme folgende Nachricht:
run:
java.lang.reflect.InvocationTargetException
at java.desktop/java.awt.EventQueue.invokeAndWait(EventQueue.java:1371)
at java.desktop/java.awt.EventQueue.invokeAndWait(EventQueue.java:1346)
at java.desktop/javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1480)
at pdffxmlcontroller.PDFFXMLController.createViewer(PDFFXMLController.java:237)
at pdffxmlcontroller.PDFFXMLController.start(PDFFXMLController.java:204)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@23.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@23.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@23.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.embed.swing.SwingNodeHelper (in unnamed module @0x3a45f731) cannot access class com.sun.javafx.scene.NodeHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene to unnamed module @0x3a45f731
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1027)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at javafx.embed.swing.SwingNode.<clinit>(SwingNode.java:136)
at pdffxmlcontroller.PDFFXMLController.lambda$createViewer$6(PDFFXMLController.java:267)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:308)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Nov. 04, 2024 9:08:09 AM org.icepdf.core.pobjects.Catalog <clinit>
INFORMATION: ICEpdf Core 7.2.1
BUILD SUCCESSFUL (total time: 5 seconds)
Wo ist das Problem hat jemand eine Idee?
Ich verwende Java 21 Swing Variante funktioniert ohne Probleme. Ich möchte aber JavaFX haben.