我有一個現有的接口,有一個JPanel顯示PDF文件。在現有的JPanel中顯示pdf的基本代碼?
在此界面中顯示pdf並不打開新窗口很重要。 如何在不使用不必要代碼(庫)的情況下如何在JPanel上顯示pdf?
我有一個現有的接口,有一個JPanel顯示PDF文件。在現有的JPanel中顯示pdf的基本代碼?
在此界面中顯示pdf並不打開新窗口很重要。 如何在不使用不必要代碼(庫)的情況下如何在JPanel上顯示pdf?
如果要渲染PDF內容並忽略原始格式(粗體,字體大小等),可以使用任何PDF解析器(PDFBox,Tika等)解析PDF,然後將字符串結果設置爲任何文本組件(JTextFiled或JTextArea)。
否則你應該使用PDF渲染庫。有一些商業圖書館。
但有個小竅門我在我的最後一個項目中使用我自己的面板中顯示PDF,像這樣:
的想法是使用內嵌在應用程序中的網絡組件,然後再傳遞該組件的文件路徑,然後網絡渲染組件會加載您的機器中可用的相應PDF渲染工具,在我的情況下,該機器具有acrobat閱讀器。
我使用這個庫從DJ項目本地搖擺: http://djproject.sourceforge.net/ns/
只是讓Web瀏覽器:
private JWebBrowser fileBrowser = new JWebBrowser();
和控制瀏覽器的外觀,然後將瀏覽器添加到您的主面板(其佈局是BorderLayout)
fileBrowser.setBarsVisible(false);
fileBrowser.setStatusBarVisible(false);
fileRenderPanel.add(fileBrowser, BorderLayout.CENTER);
那麼如果你渲染PDF文件u SE:
fileBrowser.navigate(filePath);
如果你想突出顯示PDF一些關鍵字
:
fileBrowser.navigate(filePath + "#search= " + keyword + ""); // work on acrobat reader only
,如果你想渲染其他文字(普通HTML):
fileBrowser.setHTMLContent(htmlContent);
謝謝,我會試一試。這是否也適用於Linux和其他操作系統? – thewikus 2012-03-18 21:50:30
實際上我運行它在Windows操作系統下,但理論上它會工作,因爲圖書館是跨平臺的。無論如何,你可以使用任何其他網頁瀏覽器嵌入式實現。 – 2012-03-18 21:58:21
@WajdyEssam我收到以下錯誤:'在線程中的異常「主」java.lang.NoClassDefFoundError:org/eclipse/swt/events/MouseListener'是否有可能提供完整的.java或atleast至少一個工作的? – 2013-05-15 22:08:23
我覺得最好的選擇是使用ICEpdf。
ICEpdf API是100%Java,輕量級,快速,高效且易於使用的。
ICEpdf可以作爲獨立的開源Java PDF查看器使用,也可以輕鬆嵌入到任何Java應用程序中以無縫加載或捕獲PDF文檔。除了PDF文檔渲染之外,ICEpdf功能極其豐富,可以以多種創新方式使用。
在項目中使用Maven管理,你可以包括:
<!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>${icepdf.version}</version>
<exclusions>
<exclusion>
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-viewer -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-viewer</artifactId>
<version>${icepdf.version}</version>
</dependency>
然後,你可以使用類似於以下代碼,以可視化面板的PDF:
// Instance the controller
controller = new SwingController();
// We created the SwingViewFactory configured with the controller
SwingViewBuilder factory = new SwingViewBuilder(controller);
// We use the factory to build a preconfigured JPanel
// with a full and active viewer user interface.
viewerComponentPanel = factory.buildViewerPanel();
viewerComponentPanel.setPreferredSize(new Dimension(400, 243));
viewerComponentPanel.setMaximumSize(new Dimension(400, 243));
// We add keyboard command
ComponentKeyBinding.install(controller, viewerComponentPanel);
// add interactive mouse link annotation support via callback
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
// We add the component to visualize the report
reportViewerContainer.add(viewerComponentPanel, BorderLayout.CENTER);
reportViewerContainer.invalidate();
// We open the generated document
controller.openDocument(reportLocationUri.toURL());
結果你可以得到類似於下面的東西:
希望這可以幫助你。
你的意思是隻使用核心Java嗎?如果是這樣,那麼據我所知,你不能。 – 2012-03-18 19:58:21
您可以分享任何代碼(例如,用於顯示PDF文件的此JPanel)嗎?有了它,幫助你會更容易。從這個簡短的介紹中,我只能提出一種類似於HTML框架的方法,您可以在一個窗口中顯示多個HTML文檔。 – aretai 2012-03-18 20:25:23
是的我想盡可能不使用庫,因爲有些在新窗口中打開PDF,而另一些則創建自己的JPanel。我有一個面板和一個設置的位置設置的大小。此外,如果不可能,我可以使用哪個庫在已創建的MY面板中顯示PDF? HTML框架如何幫助我,這聽起來像一個解決方案,但我不知道它是如何工作的。 – thewikus 2012-03-18 21:18:26