JavaFX : display pdf with icepdf

标签 java swing pdf javafx javafx-8

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

我是这样做的:

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();
    }

}

关于JavaFX : display pdf with icepdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611666/

相关文章:

java反射System.out.println

java - 从线程更新 Swing GUI

javascript - 来自HTML图像的jsPDF导致pdf为空

c# - iTextSharp 文本字段将字体设置为粗体

java - 在Java中创建三角形按钮

java - Hibernate boolean 字段映射问题

java - JProgressBar 更新不工作

java - 如何从java中的文本字段获取数据并将其显示到第二个表单上的jlabel中

java - BorderLayout 区域中的多个对象

javascript - 为客户端(浏览器)PDF 生成分块和传输大量数据