Sample of the Week:
The AppearanceService in the Datalogics PDF Java Toolkit makes it very easy to create the default appearances for button fields that only use a text label for their appearance. What the AppearanceService can’t do for you automatically is create the icons that you might want to use as button appearances; even in the Acrobat interface, we need to pull those in from other PDF files or images.
If you’ve done any form authoring in Adobe Acrobat, you’ll recognize the two dialog boxes below. This is the area where you define what a button will look like and what it will do. The Gist that I discuss in this article replicates the functionality of these dialogs.
We start by reading in the two PDF files that we need to run the sample; a blank input file that we are going to add a button to and the PDF file that has three pages, one page for each button icon representing the different states, Up, Over, and Down.
The we get the first page, define the location of the button we want to add, add it, and then set it to be a PushButton
At this point we now have a default button field defined in the AcroForm dictionary of the PDF file and a button field widget on the first page of the PDF. We can now create our appearance object and assign it to the button field widget. We do this by creating PDFXObjectForm objects from the PDF pages in the file that we want to get our button icons from. From the button field that we added, we can locate the corresponding widget on the first page and then assign it’s appearance.
Now we have a button that will appear differently when the user interacts with it. It has a normal (or “Up” ) state, a rollover state and a down state that appears when the user clicks it.
Finally, we need to do a little bit of clean up. Button fields can be Push Buttons, CheckBoxes, or Radio Buttons. CheckBoxes and Radio Buttons have values and possibly children, Push Buttons do not. Unfortunately, an artifact of the Java Toolkit class that creates these buttons is a set of default values that are not needed by Push Buttons and actually interfere with Acrobat’s ability to display the buttons properly. We just need to remove them and everything works.
Using the techniques outlined in this article, developers can dynamically add buttons to PDF files that respond to user interaction, submitting the PDF file back to your server, sending it via email, checking it into a document repository or just about anything else you can think of.