Base code to display pdf in existing JPanel?

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

It is important to display the PDF inside this inside, rather than opening a new window. How can I display a pdf file on JPanel without using unnecessary code (libraries), if possible?

+7
source share
3 answers

if you want to display PDF content and ignore the original format (boldness, font size, etc.), you can parse the PDF using any PDF analyzer (PDFBox, Tika .. etc.), and then specify the result of the string any text Component (JTextFiled or JTextArea).

otherwise, you should use the PDF rendering library. there are some commercial libraries for this.

but there is a little trick that I used in my last project to display PDF in my own panel, for example:

enter image description here

The idea is to use the built-in web component in your application, and then transfer the path to the file of this component, then the web rendering component will load the appropriate PDF rendering tool available on your computer, in my case the machine has an acrobat reader.

I am using this Native Swing library from a DJ project: http://djproject.sourceforge.net/ns/

just make a web browser:

private JWebBrowser fileBrowser = new JWebBrowser(); 

and control the appearance of the browser, 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 use a pdf file:

 fileBrowser.navigate(filePath); 

if you want to highlight several keywords in a PDF:

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

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

 fileBrowser.setHTMLContent(htmlContent); 
+14
source

You need to use the library to display it as jpedal or PDF rendering or multi-valued.

+1
source

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 a standalone open source open source PDF viewer, or it can be easily integrated into any Java application to seamlessly download or capture PDF documents. Besides the PDF, ICEpdf is extremely versatile and can be used in many innovative ways.

In your Maven-managed project, you can 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 can use code similar to the following to render the PDF file in the 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 can get something like the following:

Java Swing ICEpdf Viewer

Hope this helps you.

0
source

All Articles