依次代码在现有的jpanel中显示PDF?

问题描述 投票:0回答:3

如果您想呈现PDF内容并忽略原始格式(大胆,字体大小..等),则可以使用任何PDF Parser(PDFBox,Tika .. etceet)解析PDF,然后将字符串结果设置为任何文本组件(JTEXTFIELLIAL或JTEXTFIELEL或JTEXTAREA)。

,否则您应该使用PDF渲染库。有一些商业图书馆。

但是,我上一个项目中使用了小技巧,用于在我自己的面板中显示PDF,这样:
java swing pdf jpanel
3个回答
15
投票

该想法是在您的应用程序中使用嵌入式Web组件,然后将文件路径传递给该组件,然后Web渲染组件将加载机器中可用的适当的PDF渲染工具,如果我的情况下,机器具有Acrobat读取器。

我使用此库

DJ项目的本文秋千:

Http://djproject.sourceforge.net/ns/enter image description here

即可制作网络浏览器:

private JWebBrowser fileBrowser = new JWebBrowser(); 控制浏览器外观,然后将浏览器添加到主面板(其布局为borderlayout) fileBrowser.setBarsVisible(false); fileBrowser.setStatusBarVisible(false); fileRenderPanel.add(fileBrowser, BorderLayout.CENTER);

如果您渲染PDF,请使用:

fileBrowser.navigate(filePath);

如果您想突出显示PDF中的一些关键字:

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

如果您想渲染其他文本(PLAIN,HTML):

fileBrowser.setHTMLContent(htmlContent);

我认为最好的选择是使用
ICEPDF

.

ICEPDF API为100%Java,轻巧,快速,高效且易于使用。
ICEPDF可以用作独立的开源Java PDF查看器,也可以轻松地嵌入任何Java应用程序中以无缝加载或捕获PDF文档。除了PDF文档渲染之外,ICEPDF的用途非常广泛,可以以多种创新的方式使用。

6
投票

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

希望这可以帮助您。

您需要使用库来渲染JPEDAL或PDF-RENDERER或多价。

如果您愿意使用

Swt

库,则有一种非常简单的方法。您将要声明一个摆动浏览器,然后将浏览器URL设置为文件路径。
    shell = new Shell();
    shell.setSize(800, 600);
    shell.setText("PDF View");
    
    Browser browser = new Browser(shell, SWT.NONE);
    browser.setUrl("sample.pdf");

    browser.setBounds(10, 10, 764, 541);

完整的代码是:


1
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.