How To Build A PDF Button Image Carousel
Easily create an image carousel, or slide show, inside a PDF button.
You've probably interacted with an image carousel (slide show) on a website, where you view a picture in a frame and advance to the next picture with a mouse click. This structure can be easily built into a PDF using a button field. I'll walk you through this simple process, step by step.
NOTE: Because this button carousel runs on JavaScript, it may not function properly when opened with anything but an Adobe desktop PDF viewer. It may not function with web browsers, mobile devices, and non-Adobe PDF viewers.
We'll be using the following document and field methods:
doc.addIcon( )
doc.getIcon( )
field.buttonSetIcon( )
Different Methods
There are a few different ways to to create this button slide show. You can create a different button for each image, position the buttons on top of each other, hide all but one, then write a mouse up action script that shows a different button, one at a time, and hides the rest. I won't be demonstrating that method.
You can also create a different button for each image and hide them all. Then write a mouse up action script in another button that sets its image from the hidden buttons, one at a time, using the buttonSetIcon( ) method with the buttonGetIcon( ) method as the input parameter. I won't be demonstrating that method either.
While all three methods function the same, the method I'll be demonstrating is much cleaner because it only requires one button field.
Creating The Icons
The first step is to create a button field ("Button1" in this example), size and position it on the page, and set the Layout to Icon only in the Options tab of the field properties.
Next, click Choose Icon and select an image for the button, then close the properties window.
Next, run the following line of code in the JavaScript Console:
var icn=this.getField("Button1").buttonGetIcon();
this.addIcon("House 1",icn);
"House 1" is the name of the icon. Select a name that is relevant to the image because this will also be used in the tooltip of the button.
Now change the button image by selecting Choose Icon in the options tab of the field properties and selecting another icon. Run the script in the console again, while changing the icon name:
var icn=this.getField("Button1").buttonGetIcon();
this.addIcon("House 2",icn);//Icon name changed to "House 2"
Repeat changing the icon and running the script for as many images as you want in the button carousel.
Verifying The Icons
My example includes four icons. To test the icons run the following script in the console:
this.icons;
It should return an array of icon objects like this:
[object Icon="House 1"],[object Icon="House 2"],[object Icon="House 3"],[object Icon="House 4"]
Those icons are now embedded inside the PDF. If the button is cleared and the PDF is saved, the icons will not be visible when the document is reopened, but they can easily be retrieved using the getIcon( ) method.
The Button Script
The mouse up action script in the button will be an else/if statement that sets the button icon from the available icons, and changes the icon with each click. A condition is required in order to select the correct icon. Since buttons don’t have a value property, we can't use a changing button value. We would have to either use a hidden text field to store values, or use another button property that we can change. The tooltip is perfect for this because it will display the icon name when the user hovers over the field. The JavaScript field property for tooltip is called userName. The following script should be entered as mouse up action in the button field:
if(event.target.userName=="House 1")
{
//if userName is "House 1" change to "House 2"
event.target.userName="House 2";
event.target.buttonSetIcon(this.getIcon("House 2"));
} else
if(event.target.userName=="House 2")
{
//if userName is "House 2" change to "House 3"
event.target.userName="House 3";
event.target.buttonSetIcon(this.getIcon("House 3"));
} else
if(event.target.userName=="House 3")
{
//if userName is "House 3" change to "House 4"
event.target.userName="House 4";
event.target.buttonSetIcon(this.getIcon("House 4"));
}
else
{
/*if userName is not "House 1", "House 2", or "House 3", change it to "House 1" so the images advance in a continous loop.*/
event.target.userName="House 1";
event.target.buttonSetIcon(this.getIcon("House 1"));
}
The last step is to enter any one of tooltips manually in the General tab of the field properties. Notice each statement is else/if and the last one is simply else. The final else statement means if all the previous else/if statements return false, the final block of code runs, which sets the button to the first statement thereby creating a continuous loop of all four images.
Each condition is whether the tooltip is equal to a value. If it is equal to that value, the tooltip and the icon are both changed. This entire carousel only requires one button field.
You can download a working example of the slideshow here: