Java 在现有 JPanel 中显示 pdf 的基本代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9761727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic code to display a pdf in an existing JPanel?
提问by thewikus
I have an existing interface that has a JPanel for displaying pdf files.
我有一个现有的界面,它有一个用于显示 pdf 文件的 JPanel。
It is important to display the pdf inside this inteface and not open a new window. How can I display a pdf on the JPanel without using unnecessary code (libraries) if possible?
在此界面中显示 pdf 而不是打开新窗口很重要。如果可能,如何在不使用不必要的代码(库)的情况下在 JPanel 上显示 pdf?
采纳答案by Wajdy Essam
if you want to render PDF content and ignoring the orginal format (boldness, font size.. etc) you can parse PDF using any PDF parser(PDFBox, Tika .. etc) and then set the string result to any text Component (JTextFiled or JTextArea).
如果您想呈现 PDF 内容并忽略原始格式(粗体、字体大小等),您可以使用任何 PDF 解析器(PDFBox、Tika .. 等)解析 PDF,然后将字符串结果设置为任何文本组件(JTextFiled 或JTextArea)。
otherwise you should use PDF rendering library. there are some commercial libraries for that.
否则你应该使用 PDF 渲染库。有一些商业图书馆。
but there is small trick i was used in my last project to display PDF in my own panel, like this:
但是我在上一个项目中使用了一个小技巧来在我自己的面板中显示 PDF,如下所示:
the idea is use embedded web component in your application , and then pass the file path to this component, then web rendering component will load the appropriate PDF rendering tool available in your machine, in my case the machine have acrobat reader.
这个想法是在您的应用程序中使用嵌入式 Web 组件,然后将文件路径传递给该组件,然后 Web 渲染组件将加载您机器中可用的适当 PDF 渲染工具,在我的情况下,机器有 acrobat 阅读器。
i use this library Native Swing from DJ project:http://djproject.sourceforge.net/ns/
我使用来自 DJ 项目的这个库Native Swing:http : //djproject.sourceforge.net/ns/
just make web browser:
只需制作网络浏览器:
private JWebBrowser fileBrowser = new JWebBrowser();
and control the browser appearance, then add the browser to your main panel ( its layout is BorderLayout)
并控制浏览器外观,然后将浏览器添加到您的主面板(其布局为 BorderLayout)
fileBrowser.setBarsVisible(false);
fileBrowser.setStatusBarVisible(false);
fileRenderPanel.add(fileBrowser, BorderLayout.CENTER);
then if you to render PDF use:
那么如果你渲染PDF使用:
fileBrowser.navigate(filePath);
if you want to highlight some keyword in the PDF:
如果要突出显示 PDF 中的某个关键字:
fileBrowser.navigate(filePath + "#search= " + keyword + ""); // work on acrobat reader only
if you want to render other text (plain, html):
如果要呈现其他文本(纯文本、html):
fileBrowser.setHTMLContent(htmlContent);
回答by mark stephens
You need to use a library to render it like jpedal or PDF-renderer or multivalent.
您需要使用库来渲染它,如 jpedal 或 PDF 渲染器或多价。
回答by Sergio Sánchez Sánchez
I think the best alternative is to use ICEpdf.
我认为最好的选择是使用ICEpdf。
The ICEpdf API is 100% Java, lightweight, fast, efficient, and very easy to use.
ICEpdf API 是 100% Java,轻量级、快速、高效且非常易于使用。
ICEpdf can be used as standalone open source Java PDF viewer, or can be easily embedded in any Java application to seamlessly load or capture PDF documents. Beyond PDF document rendering, ICEpdf is extremely versatile, and can be used in a multitude of innovative ways.
ICEpdf 可以用作独立的开源 Java PDF 查看器,也可以轻松嵌入任何 Java 应用程序以无缝加载或捕获 PDF 文档。除了 PDF 文档渲染之外,ICEpdf 还非常通用,可以以多种创新方式使用。
In your project managed with Maven you could include:
在您使用 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>
Then, you could use a code similar to the following to visualize the pdf in a panel:
然后,您可以使用类似于以下的代码在面板中可视化 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());
As a result you could get something like the following:
因此,您可能会得到如下内容:
Hope this can help you.
希望这可以帮到你。