iText Tutorial SourceForge.net Logo iText, a Free Java-PDF library
by
Bruno Lowagie
[Home] [Previous] [TOC] [Next] [PDF]

Part I: Simple iText

Chapter 3: Anchors, Lists and Annotations


Anchors
We all know what hypertext links are in HTML. If you click on certain words, you can jump to another page on the net. This kind of functionality is also possible in PDF. In fact I wrote a whole chapter about all kinds of links in PDF: see Chapter 11, but that is rather advanced use of iText. In these first chapters we only deal with simple iText.
If you want to add an external link to a document (i.e. a link to another document on the web, using its URL), you can simply use the Anchor-object. It's derived from the Phrase-object and it's used in the same way. There are only two extra methods to define two extra variables: setName and setReference.

Example external link:
Anchor anchor = new Anchor("website", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
anchor.setReference("http://www.lowagie.com/iText/");
anchor.setName("website");


If you want to add internal links, you need to choose a unique name for this link, just like you would do in HTML and use this as name for the anchor. When you make a reference to this destination, you need to add a #-character.

Example internal link:
Anchor anchor1 = new Anchor("This is an internal link");
anchor1.setName("link1");
Anchor anchor2 = new Anchor("Click here to jump to the internal link");
anchor.setReference("#link1");


Both examples are demonstrated in Chap0301.java; you can test the result here.


Lists
With classes List and ListItem, you can add lists to a PDF file. You can choose if you want ordered or unordered lists:

Example ordered list:
List list = new List(true, 20);
list.add(new ListItem("First line"));
list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
list.add(new ListItem("Third line"));


The result of this code looks like this:
  1. First line
  2. The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?
  3. Third line
Example unordered list:
List overview = new List(false, 10);
overview.add(new ListItem("This is an item"));
overview.add("This is another item");


The result of this code looks like this:
  • This is an item
  • This is another item
You can change the way the listsymbol looks with method setListSymbol:

// set a String as listsymbol
list1.setListSymbol("*");
// set a Chunk (that contains the bullet character) as listsymbol
list2.setListSymbol(new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 20)));
// set an Images wrapped in a Chunk as listsymbol
list3.setListSymbol(new Chunk(Image.getInstance("myBullet.gif"), 0, 0));

There are also some methods to change the indentation of the list such as setIndentationLeft and setIndentationRight. The indentation of the list symbol is set in the constructor.
You can find more examples here: Chap0302.java and look at the result.


Annotations
In iText, different types of annotations are supported:
  • Text: you can add small pieces of text to your document that are not really part of the content. Annotations have a title and some content.
    Annotation a = new Annotation(
        "authors",
        "Maybe it's because I wanted to be an author myself that I wrote iText.");
  • External links: you have to specify a clickable rectangle and a String (representing the URL) or an URL-object.
    Annotation annot = new Annotation(100f, 700f, 200f, 800f, new URL("http://www.lowagie.com"));
    Annotation annot = new Annotation(100f, 700f, 200f, 800f, "http://www.lowagie.com");
  • External PDF file: you have to specify a clickable rectangle and a String (the filename) and a destination or pagenumber.
    Annotation annot = new Annotation(100f, 700f, 200f, 800f, "other.pdf", "mark");
    Annotation annot = new Annotation(100f, 700f, 200f, 800f, "other.pdf", 2);
  • Named action: you have to specify a clickable rectangle and a named action:
    Annotation annot = new Annotation(100f, 700f, 200f, 800f, PdfAction.FIRSTPAGE);
  • Application: you have to specify a clickable rectangle and an application:
    Annotation annot = new Annotation(300f, 700f, 400f, 800f, "C://winnt/notepad.exe", null, null, null);
If you look at the Chap0303.pdf is the result of example 3. We didn't need to specify a position on the page, so iText took care of this internally. You can see that iText adds the text-annotations just below the current location on the page. The first one below the first line after the paragraph. The second one just below the end of the phrase.
All other types of annotations need you to specify the coordinates of a rectangle. In example 4, we draw some squares (using functionality that is discussed in Chapter 10) and we add an annotation to each square. See Chap0304.pdf.


[Top] [Previous] [TOC] [Next] [PDF]
Page Updated: $Date: 2003/06/25 07:36:34 $
Copyright © 2000, 2001 by Bruno Lowagie
Adolf Baeyensstraat 121, 9040 Gent, BELGIUM,
tel +00 32 92 28 10 97 mailto:itext-questions@lists.sourceforge.net