Crionics LOGO

TUTORIALS

 

 Viewer Tutorial:
Using jPDF Viewer as an applet

As easy as 1, 2, 3...

This tutorial assumes you already have a basic knowledge of Swing.

 

What is an applet?

Since many readers may not be experts, we will first introduce the technology:

An applet is a Java program that can be included in an HTML page, much in the same way an image is included in a page. When you use a Java-compliant browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM). Microsoft has a similar technology called ActiveX, but that only works from Internet Explorer. Applets are not only browser independent, they also happen to be platform independent.

It's important to understand that in order to display an applet, you need at least:

* An HTTP Server
* A Browser!
* A page with an applet tag (see next section)
* And, of course, the Java applet code itself (in our case, the file jpdf-viewer.jar)

For information: Sun Microsystems, the maker of Java, has another deployment framework called Java WebStart. We will cover that technology in a different tutorial.

The applet HTML page

In this section we will show what is required to embed an applet in an HTML page. We will then see in section #3 how Java technology can help to build a more generic solution.

Let's assume the page (applet.html) and jpdf-viewer.jar are deployed in the folder associated with this url: http://myserver:8080/myfolder
You would typically call the page a link, such as http://myserver:8080/myfolder/applet.html .

But let's have a closer look at the applet page:

<html><body>

<APPLET code="com.crionics.jpdf.viewer.ViewerApplet.class"
codebase="http://myserver:8080/myfolder/"
archive="jpdf-viewer.jar"
width="100%" height="100%">

<PARAM NAME="document" VALUE="http://myserver:8080/myfolder/mydoc.pdf">
<PARAM NAME="advancedViewer" VALUE="true">

</APPLET>

</body></html>

The purpose of this tutorial is not to teach you how to build applets. (For related resources, check the Internet which is full of tutorials.) We will instead give details about applets as applied to jPDF Viewer. In the page above, code points at the applet entry point; the codebase parameter points at the URL where the archive jpdf-viewer.jar is located.

You will notice that the applet takes two parameters:

  1. document: That is, the document to display. This is typically a URL pointing at a remote PDF document. Keep in mind that applets have security restrictions: unless signed, they can only get files from the server that served them. Luckily, jpdf-viewer.jar is a signed applet.
  2. advancedViewer: A flag that sets the viewer look and feel - either minimalist or full-featured.

JSP to the rescue

The above HTML works great, but what if you move the folder to a different context (say, http://prod/viewer/)?

Well, you will need to change the codebase attribute manually. This can be a little annoying, especially if the page has moved locations frequently.

Using the following code, we can use JSP scripting to dynamically figure out the codebase for us:

<html><body>

<%
// This code automatically figures out the codebase from the requested URL.
// It requires a JSP engine:

String base=request.getRequestURL().toString();
int i=base.lastIndexOf('/');
if(i>0)
base=base.substring(0,i+1);
%>


<APPLET code="com.crionics.jpdf.viewer.ViewerApplet.class"
codebase="<%=base%>"
archive="jpdf-viewer.jar"
width="100%" height="100%">

<PARAM NAME="document" VALUE="http://myserver:8080/myfolder/mydoc.pdf">
<PARAM NAME="advancedViewer" VALUE="true">

</APPLET>

</body></html>


© Crionics Inc. Report issues with this page