Interactive Features

Revision as of 16:49, 23 May 2018 by Adminko (talk | contribs)
Jump to: navigation, search

This article describes the API related to the PDF features that allow a user to interact with a document when viewing it using the mouse and keyboard e.g. links, annotations, actions, events, various form controls etc.

Viewer preferences

The preferrred viewing parameters can be set using the document-level API described in details here.

Document-level navigation

The PDF standard describes several approaches for implementing a document-level navigation. For these purposes a few PDF objects were defined, namely Destinations and Document Outlines (sometimes referred as “Bookmarks”).


A destination defines a particular view of a document, consisting of the following items:

  • The page of the document that shall be displayed
  • The location of the document window on that page
  • The magnification factor (zoom)

Destinations may be associated with outline items (see the section 12.3.3 “Document Outline” of the specification), annotations or actions. In each case, the destination specifies the view of the document that shall be presented when the outline item or annotation is being opened or the action is being performed. A destination may be specified either explicitly, by creating an instance of the Destination class and defining its properties or indirectly, using its name.

Explicit destinations

Explicit destinations in PDF documents, as the name suggests, are defined in place, explicitly, with all the parameters needed for the navigation provided at the moment of creation.

Named destinations

Named destinations are in fact references to the destination objects present in the document's internal registry and describing the actual navigation. These destination objects should be registered in the PDF document with names, and in case of Fixed layout API, it's the FixedDocument.Destinations collection they should be added into to become usable.

The code below demonstrates how to add two link annotations onto the page of the PDF document and set their targets using explicit and named Destination objects.

// create output PDF file
using (FileStream outputStream = new FileStream("navigation.pdf", FileMode.Create, FileAccess.Write))
    // create new PDF document
    FixedDocument document = new FixedDocument();
    document.Pages.Add(new Page());
    // second page we are going to add a link to
    Page page2 = new Page();
    // create and register the named destination object
    document.Destinations.Add("myDestination",new Destination(page2, new DestinationTypeFit()));
    // add and place text object containing the text for our links
    TextObject textObject = new TextObject(StandardFonts.Helvetica, 14);
    textObject.AppendText("Navigate to page 2 using explicit destination");
    textObject.AppendText("Navigate to page 2 using named destination");
    // create link annotation using explicit destination
    LinkAnnotation linkAnnotation1 = new LinkAnnotation(new Boundary(10, 810, 300, 830));
    linkAnnotation1.BorderStyle = new AnnotationBorderStyle(1);
    linkAnnotation1.Color = new double[] { 0, 0, 0.9 };
    linkAnnotation1.Destination = new Destination(page2, new DestinationTypeFit());
    // create link annotation using named destination
    LinkAnnotation linkAnnotation2 = new LinkAnnotation(new Boundary(10, 780, 300, 800));
    linkAnnotation2.BorderStyle = new AnnotationBorderStyle(1);
    linkAnnotation2.Color = new double[] { 0, 0, 0.9 };
    linkAnnotation2.Destination = new Destination("myDestination");
    // add annotations to the PDF page

Usage of explicit destinations is pretty straightforward, while named destinations require registration in their parent document. Using named destination it becomes possible to separate references generation from actual content generation. You may generate a list of links using destinations names first and later register these destination objects pointing to the correct locations.

Resulting PDF file is shown on the image below:

Links added using explicit and named destinations

Document outline (bookmarks)

A PDF document may contain a document outline that the conforming reader may display on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of outline items (sometimes called bookmarks), which serve as a visual table of contents to display the document's structure to the user. The user may interactively open and close individual items by clicking them with the mouse. When an item is open, its immediate children in the hierarchy shall become visible on the screen, each child may in turn be opened or closed, selectively showing or hiding further parts of the document hierarchy. When an item is closed, all of its descendants in the hierarchy should become hidden. Clicking the text of any visible item shall activate the item, causing the conforming reader to jump to a destination or trigger an action associated with the item.

Document bookmarks or outlines, as they are named in PDF specification, can be enumerated, added or edited using the Bookmark class provided by the Apitron PDF Kit. The Bookmarks property of the FixedDocument class is designed for this purpose. Each bookmark element in its turn has Bookmarks property holding its children and so on. A bookmark can have either a destination or action assigned.

Consider the code below demonstrating the creation of bookmarks:

// create output PDF file
using (FileStream outputStream = new FileStream("navigation.pdf", FileMode.Create, FileAccess.Write))
    // create new PDF document
    FixedDocument document = new FixedDocument();
    // add three pages
    document.Pages.Add(new Page());
    document.Pages.Add(new Page());
    document.Pages.Add(new Page());
    // add root bookmark and set it open by default
    Bookmark rootBookmark = new Bookmark(document.Pages[0], "Table of contents");
    rootBookmark.IsOpen = true;
    // add nested bookmarks
    rootBookmark.AddLast(new Bookmark(new Destination(document.Pages[1]), "Page 2"));
    rootBookmark.AddLast(new Bookmark(new GoToAction(document.Pages[2]), "Page 3"));
    rootBookmark.AddLast(new Bookmark(new JavaScriptAction("app.alert('Hello World!');"), "JavaScript Bookmark"));

The image below shows the resulting document produced by the sample code with the TOC containing three bookmarks of different nature - first uses explicit destination, second uses GoTo action, and the last uses JavaScript action invoking the alert window:

Creating bookmarks in PDF document


Interactive forms

An interactive form sometimes referred to as an AcroForm - is a collection of fields for gathering information interactively from the user. A PDF document may contain any number of fields diplayed on any combination of pages, all of which make up a single, global interactive form spanning the entire document. Arbitrary subsets of these fields can be imported or exported from the document.

A PDF form can be seen as two separate parts - data model that use document fields to store information entered by user and its view presenting the data to the user and providing interaction.

If a field has visual representation on a PDF page, then it’s provided by using a special type of annotation object called Widget Annotation. There are widgets defined for common control elements e.g. button, checkbox, textbox etc. See section 12.7 “Interactive Forms” of the PDF specification for the detailed description of PDF forms related objects.

One can programmatically create PDF forms or edit existing forms using Apitron PDF Kit, as shown by the samples provided in the form samples section.


An FixedDocument.AcroForm property if you use Fixed layout API or FlowDocument.Fields property if you use Flow layout API can be used to read or write document's fields - objects of specific type defined by the PDF specification and corresponding data. Available field types are as follows:

  • Button fields, represent interactive controls on the screen that the user can manipulate with the pointing device. These are push-buttons, check boxes, and radio buttons.
  • Text fields are boxes in which the user can enter some text from the keyboard.
  • Choice fields contain several text items, at most one of which can be selected as the field's value. They include scrollable list boxes and combo boxes.
  • Signature fields represent the digital signatures and optional data for authenticating the signer and the validity of document’s contents.

All corresponding classes implementing form fields can be found in Apitron.PDF.Kit.Interactive.Forms namespace.

Form actions

In addition to the standard actions described earlier, interactive forms support additional actions described below:

  • submit-form action - when invoked, an interactive PDF reader should transmit the names and values of the selected interactive form fields to a specified URL
  • reset-form actionon - when invoked, an interactive PDF reader whould reset selected interactive form fields to their default values
  • import-data action - when invoked, a PDF reader whould import data from Forms Data Format (FDF), XFDF (XML-based Forms Data Format according to ISO 19444-1:2016) or any other data format it supports into the document’s interactive form from a specified file

Named pages

The optional FixedDocument.Names.Pages entry in a document’s name registry contains a map that maps name strings to individual pages within the document. Naming a page allows it to be referenced in two different ways:

  • An import-data action can add the named page to the document into which FDF is being imported, either as a page or as a button appearance
  • A script executed by an ECMAScript action can add the named page to the current document as a regular page

Non interactive forms

It's possible to have fields without any visual representation, hence available for software-only reading and writing. It could be useful for saving some specific info related to the document, e.g. in order to implement custom workflows. Otherwise it might be an interactive form, converted to use only page content. This forms can be called non-interactive forms.

Code examples

Create, save and edit PDF form

The following example shows how to create a simple PDF form, fill it with some values, save to PDF file and then load and read the stored data.

public static void CreatePDFFormAndReadItsData()
    // create output PDF file
    using (FileStream outputStream = new FileStream("forms.pdf", FileMode.Create, FileAccess.Write))
        FixedDocument document = new FixedDocument();
        document.Pages.Add(new Page());
        // create fields and add them into the document
        TextField txtName = new TextField("Name", "John Doe");
        TextField txtBirthDate = new TextField("birthDate","12.05.1975");
        PushbuttonField btnChange = new PushbuttonField("btnChange","Change");
        // create views for fields and set their properties
        TextFieldView nameView = new TextFieldView(txtName, new Boundary(10, 800, 100, 825));
        nameView.FontResourceID = "Arial";
        nameView.FontSize = 14;
        nameView.BorderStyle = new AnnotationBorderStyle(2, AnnotationBorderType.Inset);
        nameView.BorderColor = new double[]{1};
        TextFieldView birthDateView = new TextFieldView(txtBirthDate,new Boundary(10,775,100,795));
        birthDateView.FontResourceID = "Courier";
        birthDateView.FontSize = 12;
        birthDateView.BorderStyle = new AnnotationBorderStyle(1,AnnotationBorderType.Inset);
        birthDateView.BorderColor = new double[] { 1 };
        PushbuttonFieldView buttonView = new PushbuttonFieldView(btnChange, new Boundary(10, 745, 50, 770));
        // add views to the PDF page
        AnnotationCollection pageAnnottations = document.Pages[0].Annotations;

    // open file for reading
    using (FileStream inputStream = new FileStream("forms.pdf", FileMode.Open, FileAccess.Read))
        FixedDocument document = new FixedDocument(inputStream);
        // read text fields
        foreach (TextField textField in document.AcroForm.Fields.OfType<TextField>())    
            Console.WriteLine("Field \"{0}\" has value: {1}",textField.FieldName, textField.Text);

Produced PDF file looks as follows:

Create and fill the PDF form

Looking into the code we can see that fields storing form values are created first and then we create and assign views for them. After that we put these views on page using the annotations collection (because these views are essentially Widgets annotations).

The results of reading this file using Apitron PDF Kit are shown on the image below:

Read the PDF form's values

It’s possible to change the saved fields values and write the file back, thus making it possible to programmatically fill the desired PDF form.