Developing a desktop application in JavaFX
which requires to display a pdf. I read that there is no support for pdf viewing/displaying in JavaFX
(current version), I read about JPedal
too.
Now, questions:
- Is there any external component or library to view pdf in JavaFX? It should be a freeware.
- (If I have to use
JPedal
) How can I embed it in my application.
JPedalFX Sample Code and Usage
Sample code on using JPedalFX is provided with the JPedalFX download.
Kind of lame on my part, but I'll just paste snippets sample code here that have been copied from the sample viewer provided with the JPedalFX library. The code relies on the jpedal_lgpl.jar file included with the JPedalFX distribution being on the classpath (or the library path referenced in the manifest of your application jar).
Should you have further questions regarding usage of JPedalFX, I suggest you contact IDR solutions directly (they have been responsive to me in the past).
SwingLabs PDF Renderer
Additionaly, I used an old SwingLabs Swing based pdf renderer with JavaFX in the past for rendering pdf's for my JavaFX web browser. Although the Swing/JavaFX integration wasn't a supported feature of JavaFX at the time that I developed the browser, it still worked fine for me. Code for integration is in PDFViewer.java and BrowserWindow.java.
Note that embedding JavaFX in a Swing app is supported in Java 2.2 and embedding a Swing app in JavaFX is supported in Java 8.
For some folks it may be a work-around to convert the PDF document to HTML and display it with a WebView.
The Open Source command-line tool pdf2htmlEx produces really good-looking standalone HTML files with images and JavaScript embedded.
Try JPedalFX It is stated on their web site that "JPedalFX is a lightweight PDF viewer based on JavaFX 2 and the LGPL version of JPedal. It has a simple interface and is designed for quick and easy viewing of PDF documents."
http://www.idrsolutions.com/jpedalfx-viewer/
Havent tried yet but hope it helps
Ok, here is my 50 cents. In addition to @ALabrosik and @ReneEnriquez answers.
Download pdf.js dist and place it under
src/main/resources
Create the following fxml file (you should wrap WebView in TabPane or similar container to avoid problems with scrolling support)
To prevent pdf.js from opening demo pdf file on startup, open
web/viewer.js
and clearDEFAULT_URL
value.Open
web/viewer.html
and add script block:Now the controller (see code comments for explanation).
Some of pdf.js functions will not work: open file (cause pdf.js have no access to URL outside JAR), printing etc. To hide corresponding toolbar buttons you can add the following lines to web.css:
That's all. The rest of the code is trivial.
Hope this helps to someone.
ICEPDF is really easy to work with, free, and lightweight. I recently used it to make a small PDF indexing application for my company ;)