5 Adding JavaScript actions
HOOPS Publish supports the connection of JavaScript actions to fields and buttons within the PDF document.
Adobe JavaScript for Acrobat 3D is a separate JavaScript interface provided by Adobe for 3D Annotations, although it can be called from the standard Adobe Scripting engine via the JavaScript  Annot3D.context3D property. To find more resources search for "JavaScript for Acrobat 3D" on the Adobe Developer network. If viewing pages in Adobe Livedocs it is a good idea to turn on display of the contents pane using the button at the top left of the page.
HOOPS Publish does not document the JavaScript interface and Tech Soft 3D does not warrant support for it.
To familiarise yourself with the Adobe JavaScript for Acrobat 3D we suggest reading the "JavaScript for Acrobat 3D Annotations API Reference" (js_3d_api_reference.pdf) available on the Adobe Developer website.
The WorkInstruction example on the HOOPS Publish download demonstrates how to attach JavaScript to buttons to cause pre-defined Views in the model to be executed when the button is pressed.
The JavaScript code in myjs1 gets the first 3D annotation on the first page to retrieve the 3D context for that annotation. Note that the function parameter in GetAnnots3D is the zero-based page number, whilst the array index is the zero-based 3D annotation number on the page.
The runtime object represents an instance of the playback engine which manages event processing. The runtime.setView method sets the current view for the annotation, with the Boolean value indicating whether to animate to the view when it is set.
Creating a view and creating an image from a view
The UserDefinedViews example shows how to render the 3D scene to an image using a programmatically defined camera setup, and to then use the image as the icon for a button in the PDF document.
Initially some JavaScript actions, view names and button names are defined:
Then the stCreateView function defined in the sample is used to create the view. The image is created using the sample function stCreateImageFromView and then placed on the button and the JavaScript action is associated with the button. In this case, the action sets that view, but it is not necessary.
stCreateView sets up the view name, lighting model, render mode, background color and whether it is the default view or not, then creates a view and adds the view to the artwork:
Creating an image from a view in stCreateImageFromView is a two-step process. The first step involves rendering a ’snapshot’ to a file from the view position with a given width and height, and the second step involves creating a PDF Image from the rendered image file:
The resulting sample_UserDefinedViews.pdf is shown below, with the view set after Button2 has been pressed.
Playing an animation
The U3DWithAnimation sample shows how to play a pre-defined animation stored within a U3D file. The JavaScript below accesses a button by name, interrogates the button caption, and based on the result toggles between playing and pausing the animation for that 3D Annotation context.
Note that the first seven seconds of the animation stored in the U3D file don’t do anything so the animation is set to start at 8 seconds. The restart animation button simply calls the restart animation function, and sets the caption of the ’Play/Pause’ button appropriately:
You'll notice if you browse the Adobe JavaScript for 3D documentation that the context3D property of the Annot3D object does not have Play, Pause or Restart methods. In this case these methods are defined in the default JavaScript defined for the 3D Annotation. In the U3D With Animation sample this is timer.js...
... which is attached to the annotation when the Artwork is created. If we inspect this file we can see only the first animation in the scene is accessed. A TimeEventHandler object is created with an event callback that keeps track of the animation time and stops the animation if appropriate.
Populating list boxes and combo boxes
The DemoFunctionalities example creates a three page PDF. The first page creates a simple Hello World form programmatically, and pages two and three are based on a single template that contains a list box and a combo box placed at the same position on the form i.e. on top of each other.
Page two hides the list box so the combo box is shown, whilst page 3 hides the combo box so the list box is shown. Each lists each view in the 3D annotation. In both cases the sample shows how to add JavaScript to respond to selection change events, and how to access a separate text field, "SelectedItemValue" and set the selected value as the label for that field.
When used in a multipage PDF the field names from the template have the page number appended, so that "My3DWindow" becomes "My3DWindow_2" and "My3DWindow_3" respectively.
Note that this naming convention is supplied by HOOPS Publish, as Acrobat requires each field name in a PDF to be unique within the document, not just the page.
Note that the same API call is used to populate both the list box and combo box, but the JavaScript to control them is slightly different.
In the JavaScript, SelectedItemValue_2 represents a separate text field in which the result of the selection is shown. The function activateView is defined at the PDF Document level using the code:
The code for the third page is similar, except the mechanism to get the current selection in the list box is slightly different - in the case of the combo box the JavaScript responds to an event indicating a possible change in selection, in the case of the list box HOOPS Publish also sets the code so it is executed when the selection changes, but in this case the index of the current item is obtained to extract the item's value:
How and when the JavaScript is executed for each type of Page Field is documented in the JavaScript section of the HOOPS Publish Programming guide.
Note also the console.println statements that can be used for debugging when the PDF is opened in Adobe Acrobat. To view the debugger select Tools->JavaScript->JavaScript->Debugger within Acrobat.
