Basic code to display a pdf in an existing JPanel?

时间秒杀一切 提交于 2019-11-29 02:21:25

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).

otherwise you should use PDF rendering library. there are some commercial libraries for that.

but there is small trick i was used in my last project to display PDF in my own panel, like this:

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.

i use this library Native Swing from DJ project: 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)

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

then if you to render PDF use:

fileBrowser.navigate(filePath);

if you want to highlight some keyword in the PDF:

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

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

fileBrowser.setHTMLContent(htmlContent);
mark stephens

You need to use a library to render it like jpedal or PDF-renderer or multivalent.

I think the best alternative is to use ICEpdf.

The ICEpdf API is 100% Java, lightweight, fast, efficient, and very easy to use.

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.

In your project managed with Maven you could include:

<!-- 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:

// 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!