我一直在寻找一种在JavaFx应用程序中显示pdf文件的方法。我正在尝试使用icePDF做到这一点,但我似乎只找到了swing的代码。现在,我将代码插入了SwingNode并将其嵌入到窗格中,但是问题是JPanel包含的icePDF pdf阅读器不适合JavaFx窗格的大小。

这是我的方法:

private void handleApercuBtn(){
SwingController controller = new SwingController();

SwingViewBuilder factory = new SwingViewBuilder(controller);

JPanel viewerComponentPanel = factory.buildViewerPanel();

ComponentKeyBinding.install(controller, viewerComponentPanel);

controller.getDocumentViewController().setAnnotationCallback(
  new org.icepdf.ri.common.MyAnnotationCallback(
         controller.getDocumentViewController()));

final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode,viewerComponentPanel);

apercuPane.getChildren().add(swingNode);

controller.openDocument(filePath);
}

如何将包含icePDF阅读器的JPanel装入窗格(apercuPane)?我怎么不使用Swing代码来做到这一点?

最佳答案

/**
 * FXML Controller class
 *
 * @author Sudarshan
 */
public class PDFFXMLController implements Initializable {
    private static String pdfPath;
    private SwingController swingController;
    private JComponent viewerPanel;
    @FXML
    private BorderPane borderPane;
    /**
     * Initializes the controller class.
     */
  @Override
    public void initialize(URL url, ResourceBundle rb) {
     String pdfPathLoad;
    try {
        pdfPathLoad = loadPDF("http://21-04-2017/17854381660C617.pdf");
        createViewer(borderPane);
        openDocument(pdfPathLoad );
    } catch (IOException ex) {
        Logger.getLogger(PDFFXMLController.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    private void createViewer(BorderPane borderPane) {
        try {
            SwingUtilities.invokeAndWait(() -> {
                swingController = new SwingController();
                swingController.setIsEmbeddedComponent(true);
                PropertiesManager properties = new PropertiesManager(System.getProperties(),
                        ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
                properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_FIT, "false");
                properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_ROTATE, "false");
                properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_TOOL, "false");
                properties.set(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "1.25");
                properties.setBoolean(PropertiesManager.PROPERTY_SHOW_STATUSBAR_VIEWMODE, Boolean.FALSE);
                properties.set(PropertiesManager.PROPERTY_SHOW_TOOLBAR_PAGENAV, "false");
                ResourceBundle messageBundle = ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE);
                new FontPropertiesManager(properties, System.getProperties(), messageBundle);
                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);
                swingController.setToolBarVisible(false);
                swingController.setUtilityPaneVisible(false);
            });
        } catch (InterruptedException | InvocationTargetException e) {
        }
    }
    private void openDocument(String document) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                swingController.openDocument(document);
                viewerPanel.revalidate();
            }
        });
    }
    public String loadPDF(String adresse) throws IOException {
        System.out.println("In load PDf");
        if (!adresse.toLowerCase().endsWith("pdf")) {
            return null;
        }
        String fileName = adresse.substring(adresse.lastIndexOf("/") + 1,
                adresse.lastIndexOf("."));
        String suffix = adresse.substring(adresse.lastIndexOf("."),
                adresse.length());
        File temp = null;
    try {
            InputStream input = new URL(adresse).openStream();
            temp = File.createTempFile(fileName, suffix);
            temp.deleteOnExit();
            Files.copy(input, Paths.get(temp.toURI()),
                    StandardCopyOption.REPLACE_EXISTING);
    } catch (MalformedURLException ex) {
  Logger.getLogger(PDFFXMLController.class.getName()).log(Level.SEVERE, null, ex);
    }
        return temp.getAbsolutePath();
    }

}

07-24 15:15