KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > faceless > pdf > PDFBookmark


1 // $Id: PDFBookmark.java,v 1.5 2003/11/04 17:16:01 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.util.*;
6 import java.awt.Color JavaDoc;
7
8 /**
9  * <p>
10  * This class describes the <code>Bookmark</code> or <code>Outline</code>
11  * part of a PDF document - the "Table of Contents" structure that allows
12  * easy navigation through the document.
13  * </p>
14  * <p>
15  * Bookmarks are structured like a directory tree, with each node on the
16  * tree having zero or more children. The top of the tree is always the
17  * document itself.
18  * </p>
19  * <b>Example</b>
20  * <p>
21  * Here's an example of a simple bookmark tree, and the code that creates it.
22  * </p>
23  * <pre>
24  * +-- Section 1 -+-- SubSection 1.1
25  * | |
26  * | +-- SubSection 1.2
27  * |
28  * +-- Section 2
29  * </pre>
30  *
31  * <pre>
32  * PDF pdf = new PDF();
33  *
34  * // Add the top level bookmarks "Section 1" and "Section 2" to the document.
35  * List topbookmarks = pdf.getBookmarks();
36  * PDFBookmark s1 = new PDFBookmark("Section 1", someaction);
37  * PDFBookmark s2 = new PDFBookmark("Section 2", someaction);
38  * topbookmarks.add(s1);
39  * topbookmarks.add(s2);
40  *
41  * // Add the bookmarks under "Section 1"
42  * List s1bookmarks = s1.getBookmarks();
43  * PDFBookmark s11 = new PDFBookmark("SubSection 1.1", someaction);
44  * PDFBookmark s12 = new PDFBookmark("SubSection 1.2", someaction);
45  * s1bookmarks.add(s11);
46  * s1bookmarks.add(s12);
47  * </pre>
48  * <p>
49  * There is no limit to the number of bookmarks in a PDF document or to the
50  * level they are nested.
51  * </p>
52  * @version $Revision: 1.5 $
53  */

54 public class PDFBookmark extends PeeredObject
55 {
56     private final org.faceless.pdf2.PDFBookmark bookmark;
57
58     Object JavaDoc getPeer()
59     {
60         return bookmark;
61     }
62
63     PDFBookmark(org.faceless.pdf2.PDFBookmark bookmark)
64     {
65     this.bookmark=bookmark;
66     }
67
68     /**
69      * Create a new bookmark with an initial state of "closed".
70      * @param name The name of the bookmark as seen by the user. May include
71      * non-ASCII Unicode characters.
72      * @param action the action to perform when the bookmark is selected
73      */

74     public PDFBookmark(String JavaDoc name, PDFAction action)
75     {
76     this(name,action,false);
77     }
78
79     /**
80      * Create a new bookmark and set the initial state to "open" or "closed"
81      * @param name The name of the bookmark as seen by the user. May include
82      * non-ASCII Unicode characters.
83      * @param action The action to perform when the bookmark is selected
84      * @param open Whether the bookmark is open by default
85      * @since 1.0.4
86      */

87     public PDFBookmark(String JavaDoc name, PDFAction action, boolean open)
88     {
89     bookmark = new org.faceless.pdf2.PDFBookmark(name, action==null ? null : action.action, open);
90     }
91
92     /**
93      * Return the a List containing this bookmarks children. The list
94      * has zero or more elements
95      * of type <code>PDFBookmark</code>.
96      * @return the list of bookmarks
97      */

98     public List getBookmarks()
99     {
100     return new ListPeer(bookmark.getBookmarks());
101     }
102
103     /**
104      * Set the color of the Bookmark.
105      * This feature is only available in Acrobat 5 or later (PDF 1.4),
106      * but <i>should</i> be safely ignored by earlier viewers.
107      * @since 1.1
108      */

109     public void setColor(Color JavaDoc c)
110     {
111     bookmark.setColor(c);
112     }
113
114     /**
115      * Set the style of the Bookmark to <i>italic</i>, <b>bold</b> or
116      * <b><i>both</i></b>.
117      * This feature is only available in Acrobat 5 or later (PDF 1.4),
118      * but <i>should</i> be safely ignored by earlier viewers.
119      * @since 1.1
120      */

121     public void setStyle(boolean italic, boolean bold)
122     {
123     bookmark.setStyle(italic, bold);
124     }
125
126     /**
127      * Set the action this bookmark performs when selected
128      * @param action the action to perform when the bookmark is activated - may be <tt>null</tt>
129      * @since 1.1.12
130      */

131     public void setAction(PDFAction action)
132     {
133     bookmark.setAction(action==null ? null : action.action);
134     }
135
136     /**
137      * Get the action this bookmark performs when selected
138      * @return the action performed when the bookmark is activated - may be <tt>null</tt>
139      * @since 1.1.12
140      */

141     public PDFAction getAction()
142     {
143     return (PDFAction)PeeredObject.getPeer(bookmark.getAction());
144     }
145
146     /**
147      * Set the name of this bookmark
148      * @param name The name of the bookmark as seen by the user. May include
149      * non-ASCII Unicode characters.
150      * @since 1.1.12
151      */

152     public void setName(String JavaDoc name)
153     {
154     bookmark.setName(name);
155     }
156
157     /**
158      * Get the name of this bookmark
159      * @return the name of the bookmark as seen by the user
160      * @since 1.1.12
161      */

162     public String JavaDoc getName()
163     {
164     return bookmark.getName();
165     }
166
167     public String JavaDoc toString()
168     {
169         String JavaDoc s = "<bookmark name=\""+getName()+"\"";
170     if (getAction()!=null) {
171         s+=" action=\""+getAction()+"\"";
172     }
173     return s+"/>";
174     }
175 }
176
Popular Tags