Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic code to display a pdf in an existing JPanel?

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

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?

like image 683
thewikus Avatar asked Mar 18 '12 19:03

thewikus


People also ask

How do I display a PDF in iframe?

All you need to do is add #view=fitH to the end of the source attribute. That's it! fitH stands for fit horizontal, and this will make the PDF in our iframe fit the width of the screen.

How do I display a PDF in HTML?

To embed the PDF in the HTML window, point the page to a document and then specify the height and width of the PDF so the HTML window is the correct size using the code: <embed src="filename. pdf" width="500" height="375">. Note that an embedded PDF may look very different on different browsers and operating systems.


3 Answers

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:

enter image description here

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);
like image 132
Wajdy Essam Avatar answered Nov 08 '22 05:11

Wajdy Essam


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:

Java Swing ICEpdf Viewer

Hope this can help you.

like image 27
Sergio Sánchez Sánchez Avatar answered Nov 08 '22 05:11

Sergio Sánchez Sánchez


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

like image 30
mark stephens Avatar answered Nov 08 '22 05:11

mark stephens