Mariusz Saternus is a Polish Java Developer, currently working in the UK. He recently became interested in the idea of writing a hybrid Java/Flex application. He wrote an online PDF viewer with a Java back-end. This tutorial follows the steps he took during the development of the application. It is his first enterprise project and therefore it should not be seen as an expert's guide in this field (because it is not!) but an account of developing a hybrid Java/Flex application. You can contact him at mariusz AT idrsolutions DOT com. -- Geertjan Wielenga, Javalobby Zone Leader.
How does it work then?
You can see it for yourself at http://www.jpedal.org/FlexViewer/RC1/Viewer.html.
Use it to browse to a file on disk and then open it:
This is only version One to prove I could get it working. I've got big plans for later versions!
The viewer has two parts. The Java part, which handles all the hardcore PDF parsing and rendering, and the second part, the Flex front end, which is essentially a Flash GUI running in a browser. However, with Adobe AIR coming into play, there is no problem to turn it into a desktop application later.
So, how does it work internally? The user accesses the Flex application and selects the file he/she wants to view. That file is then uploaded to the server where the Java part of the application, in the form of a web service, is running. Next, using Java and a third party PDF library, we decode the page, get the needed information about the file and finally generate an image of the required page of the file we want to view. The link to that image is then sent back to the front end where the Flex app will fetch the image file from the server and display it to the user.
We have to use a third party PDF library simply because neither Flex nor Java provide the required functionality. There are a number of libraries out there to choose from: I'm going to go with JPedal because it is an easy to use API and has excellent rendering abilities. We'll also need a web server to run our Java code because we're going to expose it as a web service, for that reason I'll use Tomcat.
What tools am I going to need?
JPedal jar (more info on http://www.jpedal.org/download.php )
Adobe Flex Builder 3 (60-day trial available from http://www.adobe.com/products/flex/ )
Flex SDK (comes with Adobe Flex Builder 3)
Java JDK 1.6 or 1.5 (http://java.sun.com/javase/downloads/index.jsp )
NetBeans 6.1 get the Web & Java EE (http://download.netbeans.org/netbeans/6.1/final/)
Tomcat 6.0.16 (can get it with NetBeans as a bundle)
How to read this tutorial?
The tutorial is divided into two parts:
The parts are then divided into sections to reflect the structure of the project. I'll talk about each of the sections, commenting on important parts of the source code. At the end of each section there will be (if applicable) a link to a file with all the required code in it. The source code files will also contain comments. Whenever I mention things that need to be done in NetBeans, I'll highlight that text in blue, whereas operations performed in Flex Builder 3 will be highlighted in green. Last but not least, I'll be referring to the installation directory of tomcat as CATALINA_HOME throughout this tutorial and in the source code as well.