PDF files can be viewed in a Browser with a Java Applet using JPedal. The Viewer example can be run as an applet from a web page with no modifications. Click here to run an example page with a PDF applet viewer.
Because this applet uses the same code as our standalone version, the JPedal applet also provides text extraction, enhanced page turn, printing and all the other features that have made JPedal so popular with developers.
Important note
You should use the signed jar if you wish to run JPedal as an Applet
Sample code
To run JPedal as an applet, add the following code to your HTML page. While it is possible to use the <applet></applet> tags, these have been deprecated by W3C.
The code beneath uses the <object></object> tags, and makes use of IE's conditional commenting to make it cross browser compatible. Many thanks to Shayne Steele from Florida State University for working out the tweaks needed for this code to function.
<!--[if !IE]> Firefox and others will use outer object -->
<object align="center" height=95% width=95%
classid="java:org.jpedal.examples.viewer.AppletViewer"
type="application/x-java-applet"
archive="http://www.jpedal.org/download/jpedal.jar,
http://www.jpedal.org/download/jars/bc.jar,
http://www.jpedal.org/download/jars/itext-1.3.jar" >
<!--<![endif]-->
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
<object align="center" height=95% width=95%
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
<param name="archive" value="download/jpedal.jar,
download/jars/bc.jar, download/jars/itext-1.3.jar">
<param name="code" value="org.jpedal.examples.viewer.AppletViewer">
</object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->
Notes on the sample code
- The clsid in the IE code will use the latest installed version of Java.
- The IE conditional commenting will ensure IE does not try to run the Firefox object.
- As Firefox does not recognise the clsid tag, it will not try to run the IE object.
Assigning more memory
Sometimes you need more memory to display a PDF file. You can increase this using this code (512Megabytes of memory in this example). Click here for a useful guide on using this to set general parameters.
<param name="java_arguments" value="-Xmx512m">
Specifying a default file
It is possible to specify a default file using a 'param' tag. This should be placed in both your inner and outer objects.
<param name="openURL" value="http://www.jpedal.org/jpedal.pdf">
Loading a properties file
It is possible to specify a properties file to load using a 'param' tag by specifying the absolute file name of the file you wish to use. This should be placed in both your inner and outer objects.
<param name="propertiesFile" value="C:\Users\IDRSolutions\Documents\properties.xml">
It should be noted that the address of the properties file is resolved on the client and not the server (as this is where the applet is actually executed). Therefore paths like the one above is resolved on the client and not the server. You can read about creating and editing a properties file here.
You can set the memory mode directly in the HTML with this command
<param name="org.jpedal.memory" value="true">
Setting up the applet to allow JavaScript calls
You need to modify your Object tags in two ways, as shown below.
<!--[if !IE]> Firefox and others will use outer object -->
<object id="pdfapplet" align="center" height=95% width=95%
classid="java:org.jpedal.examples.viewer.AppletViewer"
type="application/x-java-applet"
archive="http://www.jpedal.org/download/jpedal.jar,
http://www.jpedal.org/download/jars/bc.jar,
http://www.jpedal.org/download/jars/itext-1.3.jar" >
<param name="allowJSCalls" value="true" >
<!--<![endif]-->
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
<object id="pdfapplet" align="center" height=95% width=95%
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93">
<param name="archive" value="download/jpedal.jar,
download/jars/bc.jar, download/jars/itext-1.3.jar">
<param name="code" value="org.jpedal.examples.viewer.AppletViewer">
<param name="allowJSCalls" value="true" >
</object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->
The new param tags tell the applet to allow JavaScript calls, and adding an ID allows the applet to be easily found by JavaScript.
Writing the JavaScript
Once this is completed, you can call pdfapplet.executeCommand(command, argument) in any javascript on the page in order to access any of the commands documented here.
Unlike executeCommand in Viewer and Commands, this version uses two strings as parameters instead of an int and an object array. This is due to JavaScript's inability to access the relevant constants and create complex objects. The first parameter is the name of the command you wish to execute, and the second is a string argument. If you do not need an argument, pass null. For example:
pdfapplet.executeCommand('find','search')
pdfapplet.executeCommand('openfile',null)
Calling your own commands
All you need to do to create your own commands is add a constant into org.jpedal.examples.viewer.Commands and the relevant implementation into Commands.executeCommand - the code in AppletViewer will not need to be updated.
Note on jars not used
In the interests of speed, some jars have been left off the demo. They may be required for some PDF files. See Additional optional libraries for full details.