在现有JPanel中显示pdf的基本代码? [英] Basic code to display a pdf in an existing JPanel?

查看:304
本文介绍了在现有JPanel中显示pdf的基本代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的界面,其中有一个用于显示pdf文件的JPanel。

I have an existing interface that has a JPanel for displaying pdf files.

在此界面中显示pdf并且不打开新窗口非常重要。
如果可能的话,如何在不使用不必要的代码(库)的情况下在JPanel上显示pdf?

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内容并忽略原始格式(粗体,字体大小等),您可以使用任何PDF解析器(PDFBox,Tika ..等)解析PDF,然后将字符串结果设置为任何文本组件(JTextFiled或JTextArea) 。

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渲染库。有一些商业图书馆。

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:

这个想法是在你的应用程序中使用嵌入式web组件,然后将文件路径传递给这个组件,然后web渲染组件将加载你机器中可用的相应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.

我使用这个库来自DJ项目的Native Swing:
http://djproject.sourceforge.net/ns/

只需制作网页浏览器:

private JWebBrowser fileBrowser = new JWebBrowser();

并控制浏览器外观,然后将浏览器添加到主面板(其布局为BorderLayout)

and control the browser appearance, then add the browser to your main panel ( its layout is BorderLayout)

fileBrowser.setBarsVisible(false);
fileBrowser.setStatusBarVisible(false);
fileRenderPanel.add(fileBrowser, BorderLayout.CENTER);

然后如果你要使用PDF:

then if you to render PDF use:

fileBrowser.navigate(filePath);

如果你想在PDF中突出显示某些关键字:

if you want to highlight some keyword in the PDF:

fileBrowser.navigate(filePath + "#search= " + keyword + ""); // work on acrobat reader only

如果你想渲染其他文本(plain,html): / p>

if you want to render other text (plain, html):

fileBrowser.setHTMLContent(htmlContent);

这篇关于在现有JPanel中显示pdf的基本代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆