001: /*
002: * $Id: PdfOutline.java 2697 2007-04-19 11:59:59Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.awt.Color;
054: import java.io.IOException;
055: import java.io.OutputStream;
056: import java.util.ArrayList;
057: import java.util.Iterator;
058:
059: import com.lowagie.text.Chunk;
060: import com.lowagie.text.Font;
061: import com.lowagie.text.Paragraph;
062:
063: /**
064: * <CODE>PdfOutline</CODE> is an object that represents a PDF outline entry.
065: * <P>
066: * An outline allows a user to access views of a document by name.<BR>
067: * This object is described in the 'Portable Document Format Reference Manual version 1.3'
068: * section 6.7 (page 104-106)
069: *
070: * @see PdfDictionary
071: */
072:
073: public class PdfOutline extends PdfDictionary {
074:
075: // membervariables
076:
077: /** the <CODE>PdfIndirectReference</CODE> of this object */
078: private PdfIndirectReference reference;
079:
080: /** value of the <B>Count</B>-key */
081: private int count = 0;
082:
083: /** value of the <B>Parent</B>-key */
084: private PdfOutline parent;
085:
086: /** value of the <B>Destination</B>-key */
087: private PdfDestination destination;
088:
089: /** The <CODE>PdfAction</CODE> for this outline.
090: */
091: private PdfAction action;
092:
093: protected ArrayList kids = new ArrayList();
094:
095: protected PdfWriter writer;
096:
097: /** Holds value of property tag. */
098: private String tag;
099:
100: /** Holds value of property open. */
101: private boolean open;
102:
103: /** Holds value of property color. */
104: private Color 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 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,
152: String title, boolean open) {
153: super ();
154: this .action = action;
155: initOutline(parent, title, open);
156: }
157:
158: /**
159: * Constructs a <CODE>PdfOutline</CODE>.
160: * <P>
161: * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
162: * <CODE>true</CODE>.
163: *
164: * @param parent the parent of this outline item
165: * @param destination the destination for this outline item
166: * @param title the title of this outline item
167: */
168:
169: public PdfOutline(PdfOutline parent, PdfDestination destination,
170: String title) {
171: this (parent, destination, title, true);
172: }
173:
174: /**
175: * Constructs a <CODE>PdfOutline</CODE>.
176: * <P>
177: * This is the constructor for an <CODE>outline entry</CODE>.
178: *
179: * @param parent the parent of this outline item
180: * @param destination the destination for this outline item
181: * @param title the title of this outline item
182: * @param open <CODE>true</CODE> if the children are visible
183: */
184: public PdfOutline(PdfOutline parent, PdfDestination destination,
185: String title, boolean open) {
186: super ();
187: this .destination = destination;
188: initOutline(parent, title, open);
189: }
190:
191: /**
192: * Constructs a <CODE>PdfOutline</CODE>.
193: * <P>
194: * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
195: * <CODE>true</CODE>.
196: *
197: * @param parent the parent of this outline item
198: * @param action the <CODE>PdfAction</CODE> for this outline item
199: * @param title the title of this outline item
200: */
201: public PdfOutline(PdfOutline parent, PdfAction action,
202: PdfString title) {
203: this (parent, action, title, true);
204: }
205:
206: /**
207: * Constructs a <CODE>PdfOutline</CODE>.
208: * <P>
209: * This is the constructor for an <CODE>outline entry</CODE>.
210: *
211: * @param parent the parent of this outline item
212: * @param action the <CODE>PdfAction</CODE> for this outline item
213: * @param title the title of this outline item
214: * @param open <CODE>true</CODE> if the children are visible
215: */
216: public PdfOutline(PdfOutline parent, PdfAction action,
217: PdfString title, boolean open) {
218: this (parent, action, title.toString(), open);
219: }
220:
221: /**
222: * Constructs a <CODE>PdfOutline</CODE>.
223: * <P>
224: * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
225: * <CODE>true</CODE>.
226: *
227: * @param parent the parent of this outline item
228: * @param destination the destination for this outline item
229: * @param title the title of this outline item
230: */
231:
232: public PdfOutline(PdfOutline parent, PdfDestination destination,
233: PdfString title) {
234: this (parent, destination, title, true);
235: }
236:
237: /**
238: * Constructs a <CODE>PdfOutline</CODE>.
239: * <P>
240: * This is the constructor for an <CODE>outline entry</CODE>.
241: *
242: * @param parent the parent of this outline item
243: * @param destination the destination for this outline item
244: * @param title the title of this outline item
245: * @param open <CODE>true</CODE> if the children are visible
246: */
247: public PdfOutline(PdfOutline parent, PdfDestination destination,
248: PdfString title, boolean open) {
249: this (parent, destination, title.toString(), true);
250: }
251:
252: /**
253: * Constructs a <CODE>PdfOutline</CODE>.
254: * <P>
255: * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
256: * <CODE>true</CODE>.
257: *
258: * @param parent the parent of this outline item
259: * @param action the <CODE>PdfAction</CODE> for this outline item
260: * @param title the title of this outline item
261: */
262:
263: public PdfOutline(PdfOutline parent, PdfAction action,
264: Paragraph title) {
265: this (parent, action, title, true);
266: }
267:
268: /**
269: * Constructs a <CODE>PdfOutline</CODE>.
270: * <P>
271: * This is the constructor for an <CODE>outline entry</CODE>.
272: *
273: * @param parent the parent of this outline item
274: * @param action the <CODE>PdfAction</CODE> for this outline item
275: * @param title the title of this outline item
276: * @param open <CODE>true</CODE> if the children are visible
277: */
278: public PdfOutline(PdfOutline parent, PdfAction action,
279: Paragraph title, boolean open) {
280: super ();
281: StringBuffer buf = new StringBuffer();
282: for (Iterator i = title.getChunks().iterator(); i.hasNext();) {
283: Chunk chunk = (Chunk) i.next();
284: buf.append(chunk.getContent());
285: }
286: this .action = action;
287: initOutline(parent, buf.toString(), open);
288: }
289:
290: /**
291: * Constructs a <CODE>PdfOutline</CODE>.
292: * <P>
293: * This is the constructor for an <CODE>outline entry</CODE>. The open mode is
294: * <CODE>true</CODE>.
295: *
296: * @param parent the parent of this outline item
297: * @param destination the destination for this outline item
298: * @param title the title of this outline item
299: */
300:
301: public PdfOutline(PdfOutline parent, PdfDestination destination,
302: Paragraph title) {
303: this (parent, destination, title, true);
304: }
305:
306: /**
307: * Constructs a <CODE>PdfOutline</CODE>.
308: * <P>
309: * This is the constructor for an <CODE>outline entry</CODE>.
310: *
311: * @param parent the parent of this outline item
312: * @param destination the destination for this outline item
313: * @param title the title of this outline item
314: * @param open <CODE>true</CODE> if the children are visible
315: */
316: public PdfOutline(PdfOutline parent, PdfDestination destination,
317: Paragraph title, boolean open) {
318: super ();
319: StringBuffer buf = new StringBuffer();
320: for (Iterator i = title.getChunks().iterator(); i.hasNext();) {
321: Chunk chunk = (Chunk) i.next();
322: buf.append(chunk.getContent());
323: }
324: this .destination = destination;
325: initOutline(parent, buf.toString(), open);
326: }
327:
328: // methods
329:
330: /** Helper for the constructors.
331: * @param parent the parent outline
332: * @param title the title for this outline
333: * @param open <CODE>true</CODE> if the children are visible
334: */
335: void initOutline(PdfOutline parent, String title, boolean open) {
336: this .open = open;
337: this .parent = parent;
338: writer = parent.writer;
339: put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE));
340: parent.addKid(this );
341: if (destination != null && !destination.hasPage()) // bugfix Finn Bock
342: setDestinationPage(writer.getCurrentPage());
343: }
344:
345: /**
346: * Sets the indirect reference of this <CODE>PdfOutline</CODE>.
347: *
348: * @param reference the <CODE>PdfIndirectReference</CODE> to this outline.
349: */
350:
351: public void setIndirectReference(PdfIndirectReference reference) {
352: this .reference = reference;
353: }
354:
355: /**
356: * Gets the indirect reference of this <CODE>PdfOutline</CODE>.
357: *
358: * @return the <CODE>PdfIndirectReference</CODE> to this outline.
359: */
360:
361: public PdfIndirectReference indirectReference() {
362: return reference;
363: }
364:
365: /**
366: * Gets the parent of this <CODE>PdfOutline</CODE>.
367: *
368: * @return the <CODE>PdfOutline</CODE> that is the parent of this outline.
369: */
370:
371: public PdfOutline parent() {
372: return parent;
373: }
374:
375: /**
376: * Set the page of the <CODE>PdfDestination</CODE>-object.
377: *
378: * @param pageReference indirect reference to the page
379: * @return <CODE>true</CODE> if this page was set as the <CODE>PdfDestination</CODE>-page.
380: */
381:
382: public boolean setDestinationPage(PdfIndirectReference pageReference) {
383: if (destination == null) {
384: return false;
385: }
386: return destination.addPage(pageReference);
387: }
388:
389: /**
390: * Gets the destination for this outline.
391: * @return the destination
392: */
393: public PdfDestination getPdfDestination() {
394: return destination;
395: }
396:
397: int getCount() {
398: return count;
399: }
400:
401: void setCount(int count) {
402: this .count = count;
403: }
404:
405: /**
406: * returns the level of this outline.
407: *
408: * @return a level
409: */
410:
411: public int level() {
412: if (parent == null) {
413: return 0;
414: }
415: return (parent.level() + 1);
416: }
417:
418: /**
419: * Returns the PDF representation of this <CODE>PdfOutline</CODE>.
420: *
421: * @param writer the encryption information
422: * @param os
423: * @throws IOException
424: */
425:
426: public void toPdf(PdfWriter writer, OutputStream os)
427: throws IOException {
428: if (color != null && !color.equals(Color.black)) {
429: put(PdfName.C, new PdfArray(new float[] {
430: color.getRed() / 255f, color.getGreen() / 255f,
431: color.getBlue() / 255f }));
432: }
433: int flag = 0;
434: if ((style & Font.BOLD) != 0)
435: flag |= 2;
436: if ((style & Font.ITALIC) != 0)
437: flag |= 1;
438: if (flag != 0)
439: put(PdfName.F, new PdfNumber(flag));
440: if (parent != null) {
441: put(PdfName.PARENT, parent.indirectReference());
442: }
443: if (destination != null && destination.hasPage()) {
444: put(PdfName.DEST, destination);
445: }
446: if (action != null)
447: put(PdfName.A, action);
448: if (count != 0) {
449: put(PdfName.COUNT, new PdfNumber(count));
450: }
451: super .toPdf(writer, os);
452: }
453:
454: /**
455: * Adds a kid to the outline
456: * @param outline
457: */
458: public void addKid(PdfOutline outline) {
459: kids.add(outline);
460: }
461:
462: /**
463: * Returns the kids of this outline
464: * @return an ArrayList with PdfOutlines
465: */
466: public ArrayList getKids() {
467: return kids;
468: }
469:
470: /**
471: * Sets the kids of this outline
472: * @param kids
473: */
474: public void setKids(ArrayList kids) {
475: this .kids = kids;
476: }
477:
478: /** Getter for property tag.
479: * @return Value of property tag.
480: */
481: public String getTag() {
482: return tag;
483: }
484:
485: /** Setter for property tag.
486: * @param tag New value of property tag.
487: */
488: public void setTag(String tag) {
489: this .tag = tag;
490: }
491:
492: /**
493: * Gets the title of this outline
494: * @return the title as a String
495: */
496: public String getTitle() {
497: PdfString title = (PdfString) get(PdfName.TITLE);
498: return title.toString();
499: }
500:
501: /**
502: * Sets the title of this outline
503: * @param title
504: */
505: public void setTitle(String title) {
506: put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE));
507: }
508:
509: /** Getter for property open.
510: * @return Value of property open.
511: */
512: public boolean isOpen() {
513: return open;
514: }
515:
516: /** Setter for property open.
517: * @param open New value of property open.
518: */
519: public void setOpen(boolean open) {
520: this .open = open;
521: }
522:
523: /** Getter for property color.
524: * @return Value of property color.
525: *
526: */
527: public Color getColor() {
528: return this .color;
529: }
530:
531: /** Setter for property color.
532: * @param color New value of property color.
533: *
534: */
535: public void setColor(Color color) {
536: this .color = color;
537: }
538:
539: /** Getter for property style.
540: * @return Value of property style.
541: *
542: */
543: public int getStyle() {
544: return this .style;
545: }
546:
547: /** Setter for property style.
548: * @param style New value of property style.
549: *
550: */
551: public void setStyle(int style) {
552: this.style = style;
553: }
554:
555: }
|