Call Java PDF applet viewer commands from JavaScript
After setting up your applet, you might want to integrate it into your webpage using JavaScript. We provide the option to allow access to a number of useful commands for exactly this purpose.
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.simpleviewer.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.simpleviewer.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 SimpleViewer 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.simpleviewer.Commands and the relevant implementation into Commands.executeCommand - the code in AppletViewer will not need to be updated.
Links to related articles
Getting started tutorial
Running SimpleViewer as an Applet
JVM options available on startup which can customise memory usage, image quality and lots of other settings.
Demo of drag and drop functionality in PDF Viewer (works on applet).
AppletViewer - provides a version of SimpleViewer running as an Applet
Additional optional libraries lists items which may be needed for some PDFs or functionality.
Return to main PDF support section
Click here for the main PDF support area with lots of java examples, PDF tutorials and useful information to get the most out of the JPedal PDF library




