【问题标题】:Using ICEpdf "inside" java program使用 ICEpdf“内部”java 程序
【发布时间】:2016-05-23 17:31:12
【问题描述】:

我们已经创建了一个程序作为 uni assignment,并希望使用 ICEpdf 或其他一些用于 java 的 pdf 查看器。

我们创建了不同的选项卡,用户可以在其中选择要“上传”和查看的 pdf 文件。问题是我们希望让用户可以在应用程序中查看 pdf,而 ICEpdf 当前创建一个额外的窗口并单独显示 pdf。

这可以用 ICEpdf 查看器组件实现吗?

附上一些图片来帮助解释发生了什么。

编辑/更新:基本上 ICEpdf 需要一个 jframe,而我们需要它在当前窗口内运行。

【问题讨论】:

    标签: java pdf icepdf


    【解决方案1】:
    You will need to get border pane in your current window pane   
     /**
         * 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();
            }
    
        }
    

    【讨论】:

      【解决方案2】:

      可以查看 ICEpdf 包中 ./examples/component/ViewerComponentExample.java 中的示例。它将演示如何创建可嵌入的 JPanel 并设置键盘命令。

      【讨论】:

        【解决方案3】:

        我们想做的事情不会/不能工作,所以我们只是通过程序内的按钮在自己的窗口中打开它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多