KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfOutline


1 /*
2  * $Id: PdfOutline.java 2697 2007-04-19 11:59:59Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.awt.Color JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 import com.lowagie.text.Chunk;
60 import com.lowagie.text.Font;
61 import com.lowagie.text.Paragraph;
62
63 /**
64  * <CODE>PdfOutline</CODE> is an object that represents a PDF outline entry.
65  * <P>
66  * An outline allows a user to access views of a document by name.<BR>
67  * This object is described in the 'Portable Document Format Reference Manual version 1.3'
68  * section 6.7 (page 104-106)
69  *
70  * @see PdfDictionary
71  */

72
73 public class PdfOutline extends PdfDictionary {
74     
75     // membervariables
76

77     /** the <CODE>PdfIndirectReference</CODE> of this object */
78     private PdfIndirectReference reference;
79     
80     /** value of the <B>Count</B>-key */
81     private int count = 0;
82     
83     /** value of the <B>Parent</B>-key */
84     private PdfOutline parent;
85     
86     /** value of the <B>Destination</B>-key */
87     private PdfDestination destination;
88     
89     /** The <CODE>PdfAction</CODE> for this outline.
90      */

91     private PdfAction action;
92        
93     protected ArrayList JavaDoc kids = new ArrayList JavaDoc();
94     
95     protected PdfWriter writer;
96     
97     /** Holds value of property tag. */
98     private String JavaDoc tag;
99     
100     /** Holds value of property open. */
101     private boolean open;
102     
103     /** Holds value of property color. */
104     private Color JavaDoc color;
105     
106     /** Holds value of property style. */
107     private int style = 0;
108     
109     // constructors
110

111     /**
112      * Constructs a <CODE>PdfOutline</CODE>.
113      * <P>
114      * This is the constructor for the <CODE>outlines object</CODE>.
115      *
116      * @param writer The PdfWriter you are adding the outline to
117      */

118     
119     PdfOutline(PdfWriter writer) {
120         super(OUTLINES);
121         open = true;
122         parent = null;
123         this.writer = writer;
124     }
125     
126     /**
127      * Constructs a <CODE>PdfOutline</CODE>.
128      * <P>
129      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
130      * <CODE>true</CODE>.
131      *
132      * @param parent the parent of this outline item
133      * @param action the <CODE>PdfAction</CODE> for this outline item
134      * @param title the title of this outline item
135      */

136     
137     public PdfOutline(PdfOutline parent, PdfAction action, String JavaDoc title) {
138         this(parent, action, title, true);
139     }
140     
141     /**
142      * Constructs a <CODE>PdfOutline</CODE>.
143      * <P>
144      * This is the constructor for an <CODE>outline entry</CODE>.
145      *
146      * @param parent the parent of this outline item
147      * @param action the <CODE>PdfAction</CODE> for this outline item
148      * @param title the title of this outline item
149      * @param open <CODE>true</CODE> if the children are visible
150      */

151     public PdfOutline(PdfOutline parent, PdfAction action, String JavaDoc title, boolean open) {
152         super();
153         this.action = action;
154         initOutline(parent, title, open);
155     }
156     
157     /**
158      * Constructs a <CODE>PdfOutline</CODE>.
159      * <P>
160      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
161      * <CODE>true</CODE>.
162      *
163      * @param parent the parent of this outline item
164      * @param destination the destination for this outline item
165      * @param title the title of this outline item
166      */

167     
168     public PdfOutline(PdfOutline parent, PdfDestination destination, String JavaDoc title) {
169         this(parent, destination, title, true);
170     }
171     
172     /**
173      * Constructs a <CODE>PdfOutline</CODE>.
174      * <P>
175      * This is the constructor for an <CODE>outline entry</CODE>.
176      *
177      * @param parent the parent of this outline item
178      * @param destination the destination for this outline item
179      * @param title the title of this outline item
180      * @param open <CODE>true</CODE> if the children are visible
181      */

182     public PdfOutline(PdfOutline parent, PdfDestination destination, String JavaDoc title, boolean open) {
183         super();
184         this.destination = destination;
185         initOutline(parent, title, open);
186     }
187     
188     /**
189      * Constructs a <CODE>PdfOutline</CODE>.
190      * <P>
191      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
192      * <CODE>true</CODE>.
193      *
194      * @param parent the parent of this outline item
195      * @param action the <CODE>PdfAction</CODE> for this outline item
196      * @param title the title of this outline item
197      */

198     public PdfOutline(PdfOutline parent, PdfAction action, PdfString title) {
199         this(parent, action, title, true);
200     }
201     
202     /**
203      * Constructs a <CODE>PdfOutline</CODE>.
204      * <P>
205      * This is the constructor for an <CODE>outline entry</CODE>.
206      *
207      * @param parent the parent of this outline item
208      * @param action the <CODE>PdfAction</CODE> for this outline item
209      * @param title the title of this outline item
210      * @param open <CODE>true</CODE> if the children are visible
211      */

212     public PdfOutline(PdfOutline parent, PdfAction action, PdfString title, boolean open) {
213         this(parent, action, title.toString(), open);
214     }
215     
216     /**
217      * Constructs a <CODE>PdfOutline</CODE>.
218      * <P>
219      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
220      * <CODE>true</CODE>.
221      *
222      * @param parent the parent of this outline item
223      * @param destination the destination for this outline item
224      * @param title the title of this outline item
225      */

226     
227     public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title) {
228         this(parent, destination, title, true);
229     }
230     
231     /**
232      * Constructs a <CODE>PdfOutline</CODE>.
233      * <P>
234      * This is the constructor for an <CODE>outline entry</CODE>.
235      *
236      * @param parent the parent of this outline item
237      * @param destination the destination for this outline item
238      * @param title the title of this outline item
239      * @param open <CODE>true</CODE> if the children are visible
240      */

241     public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title, boolean open) {
242         this(parent, destination, title.toString(), true);
243     }
244     
245     /**
246      * Constructs a <CODE>PdfOutline</CODE>.
247      * <P>
248      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
249      * <CODE>true</CODE>.
250      *
251      * @param parent the parent of this outline item
252      * @param action the <CODE>PdfAction</CODE> for this outline item
253      * @param title the title of this outline item
254      */

255     
256     public PdfOutline(PdfOutline parent, PdfAction action, Paragraph title) {
257         this(parent, action, title, true);
258     }
259     
260     /**
261      * Constructs a <CODE>PdfOutline</CODE>.
262      * <P>
263      * This is the constructor for an <CODE>outline entry</CODE>.
264      *
265      * @param parent the parent of this outline item
266      * @param action the <CODE>PdfAction</CODE> for this outline item
267      * @param title the title of this outline item
268      * @param open <CODE>true</CODE> if the children are visible
269      */

270     public PdfOutline(PdfOutline parent, PdfAction action, Paragraph title, boolean open) {
271         super();
272         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
273         for (Iterator JavaDoc i = title.getChunks().iterator(); i.hasNext(); ) {
274             Chunk chunk = (Chunk) i.next();
275             buf.append(chunk.getContent());
276         }
277         this.action = action;
278         initOutline(parent, buf.toString(), open);
279     }
280     
281     /**
282      * Constructs a <CODE>PdfOutline</CODE>.
283      * <P>
284      * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
285      * <CODE>true</CODE>.
286      *
287      * @param parent the parent of this outline item
288      * @param destination the destination for this outline item
289      * @param title the title of this outline item
290      */

291     
292     public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title) {
293         this(parent, destination, title, true);
294     }
295     
296     /**
297      * Constructs a <CODE>PdfOutline</CODE>.
298      * <P>
299      * This is the constructor for an <CODE>outline entry</CODE>.
300      *
301      * @param parent the parent of this outline item
302      * @param destination the destination for this outline item
303      * @param title the title of this outline item
304      * @param open <CODE>true</CODE> if the children are visible
305      */

306     public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title, boolean open) {
307         super();
308         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
309         for (Iterator JavaDoc i = title.getChunks().iterator(); i.hasNext(); ) {
310             Chunk chunk = (Chunk) i.next();
311             buf.append(chunk.getContent());
312         }
313         this.destination = destination;
314         initOutline(parent, buf.toString(), open);
315     }
316     
317     
318     // methods
319

320     /** Helper for the constructors.
321      * @param parent the parent outline
322      * @param title the title for this outline
323      * @param open <CODE>true</CODE> if the children are visible
324      */

325     void initOutline(PdfOutline parent, String JavaDoc title, boolean open) {
326         this.open = open;
327         this.parent = parent;
328         writer = parent.writer;
329         put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE));
330         parent.addKid(this);
331         if (destination != null && !destination.hasPage()) // bugfix Finn Bock
332
setDestinationPage(writer.getCurrentPage());
333     }
334     
335     /**
336      * Sets the indirect reference of this <CODE>PdfOutline</CODE>.
337      *
338      * @param reference the <CODE>PdfIndirectReference</CODE> to this outline.
339      */

340     
341     public void setIndirectReference(PdfIndirectReference reference) {
342         this.reference = reference;
343     }
344     
345     /**
346      * Gets the indirect reference of this <CODE>PdfOutline</CODE>.
347      *
348      * @return the <CODE>PdfIndirectReference</CODE> to this outline.
349      */

350     
351     public PdfIndirectReference indirectReference() {
352         return reference;
353     }
354     
355     /**
356      * Gets the parent of this <CODE>PdfOutline</CODE>.
357      *
358      * @return the <CODE>PdfOutline</CODE> that is the parent of this outline.
359      */

360     
361     public PdfOutline parent() {
362         return parent;
363     }
364     
365     /**
366      * Set the page of the <CODE>PdfDestination</CODE>-object.
367      *
368      * @param pageReference indirect reference to the page
369      * @return <CODE>true</CODE> if this page was set as the <CODE>PdfDestination</CODE>-page.
370      */

371     
372     public boolean setDestinationPage(PdfIndirectReference pageReference) {
373         if (destination == null) {
374             return false;
375         }
376         return destination.addPage(pageReference);
377     }
378     
379     /**
380      * Gets the destination for this outline.
381      * @return the destination
382      */

383     public PdfDestination getPdfDestination() {
384         return destination;
385     }
386     
387     int getCount() {
388         return count;
389     }
390
391     void setCount(int count) {
392         this.count = count;
393     }
394     
395     /**
396      * returns the level of this outline.
397      *
398      * @return a level
399      */

400     
401     public int level() {
402         if (parent == null) {
403             return 0;
404         }
405         return (parent.level() + 1);
406     }
407     
408     /**
409      * Returns the PDF representation of this <CODE>PdfOutline</CODE>.
410      *
411      * @param writer the encryption information
412      * @param os
413      * @throws IOException
414      */

415     
416     public void toPdf(PdfWriter writer, OutputStream JavaDoc os) throws IOException JavaDoc {
417         if (color != null && !color.equals(Color.black)) {
418             put(PdfName.C, new PdfArray(new float[]{color.getRed()/255f,color.getGreen()/255f,color.getBlue()/255f}));
419         }
420         int flag = 0;
421         if ((style & Font.BOLD) != 0)
422             flag |= 2;
423         if ((style & Font.ITALIC) != 0)
424             flag |= 1;
425         if (flag != 0)
426             put(PdfName.F, new PdfNumber(flag));
427         if (parent != null) {
428             put(PdfName.PARENT, parent.indirectReference());
429         }
430         if (destination != null && destination.hasPage()) {
431             put(PdfName.DEST, destination);
432         }
433         if (action != null)
434             put(PdfName.A, action);
435         if (count != 0) {
436             put(PdfName.COUNT, new PdfNumber(count));
437         }
438         super.toPdf(writer, os);
439     }
440     
441     /**
442      * Adds a kid to the outline
443      * @param outline
444      */

445     public void addKid(PdfOutline outline) {
446         kids.add(outline);
447     }
448     
449     /**
450      * Returns the kids of this outline
451      * @return an ArrayList with PdfOutlines
452      */

453     public ArrayList JavaDoc getKids() {
454         return kids;
455     }
456     
457     /**
458      * Sets the kids of this outline
459      * @param kids
460      */

461     public void setKids(ArrayList JavaDoc kids) {
462         this.kids = kids;
463     }
464     
465     /** Getter for property tag.
466      * @return Value of property tag.
467      */

468     public String JavaDoc getTag() {
469         return tag;
470     }
471     
472     /** Setter for property tag.
473      * @param tag New value of property tag.
474      */

475     public void setTag(String JavaDoc tag) {
476         this.tag = tag;
477     }
478     
479     /**
480      * Gets the title of this outline
481      * @return the title as a String
482      */

483     public String JavaDoc getTitle() {
484         PdfString title = (PdfString)get(PdfName.TITLE);
485         return title.toString();
486     }
487     
488     /**
489      * Sets the title of this outline
490      * @param title
491      */

492     public void setTitle(String JavaDoc title) {
493         put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE));
494     }
495     
496     /** Getter for property open.
497      * @return Value of property open.
498      */

499     public boolean isOpen() {
500         return open;
501     }
502     
503     /** Setter for property open.
504      * @param open New value of property open.
505      */

506     public void setOpen(boolean open) {
507         this.open = open;
508     }
509     
510     /** Getter for property color.
511      * @return Value of property color.
512      *
513      */

514     public Color JavaDoc getColor() {
515         return this.color;
516     }
517     
518     /** Setter for property color.
519      * @param color New value of property color.
520      *
521      */

522     public void setColor(Color JavaDoc color) {
523         this.color = color;
524     }
525     
526     /** Getter for property style.
527      * @return Value of property style.
528      *
529      */

530     public int getStyle() {
531         return this.style;
532     }
533     
534     /** Setter for property style.
535      * @param style New value of property style.
536      *
537      */

538     public void setStyle(int style) {
539         this.style = style;
540     }
541     
542 }
Popular Tags