Site iconJava PDF Blog

How do PDF files add interactive elements?

One of the really useful features of the PDF file format is the ability to have interactive elements. These began as just simple checkboxes, buttons, comboboxes and textfields type widgets and the list has expanded to include the ability to embed Sounds, Movies and even other files or URLs. This makes the PDF file format a very interactive medium.

Here is one of my favorite examples:

All of these interactive features can be defined in 2 ways. Firstly, they can exist in  as PDF objects defined within the PDF file and they inherit values from their parent objects. It uses the standard PDF Cos format and would look something like this

26 0 obj
<<
/F 4
/I[1]
/Type/Annot
/Rect[196 594 314 613]
/BS<</W 1/S/U>>
/FT/Ch/
Subtype/Widget
/P 24 0 R
/T(Item)
/V(Soft Taco)
/AP<</N 142 0 R>>
/Ff 393216
/MK<</BC[0 0 0]>>
/Opt[(Burrito)(Soft Taco)(Mexico City)(Quesadilla)(Taquitaco)]
/DA(/TiRo 0 Tf 0 0 1 rg)
>>endobj

Or they can appear in one of several XML structures inside the file. Here is an example – the actual XML is buried inside streams in the referenced objects.

<<
/XFA[(preamble)40 0 R
(config)41 0 R
(template)42 0 R

You can also define as both a Cos object with data in the XFA – the spec is nothing if not flexible!

(datasets)43 0 R
(localeSet)44 0 R
(postamble)45 0 R]

Forms can also be linked to events and to Javascript code inside the PDF and can have tooltips, change their visibilty and interact with other components. They can also reference widgets on other pages. So there is not much you cannot achieve with them…

The thing that I found most confusing when starting was that interactive elements can actually be referenced in 2 separate ways. A PDF document can have a single Acroform or XFA object (which lists all the widgets in the document), but each page can also have a Annots object which lists the widgets on that page. So you need to potentially look at both lists and then workout which are used on any page.

So if you think PDF files are about just static WYSIWYG documents, you have been missing a whole dimension. Give them a try.