KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > documentnavigation > outline > PDOutlineItem


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.documentnavigation.outline;
32
33 import java.awt.Color JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.pdfbox.cos.COSArray;
38 import org.pdfbox.cos.COSDictionary;
39 import org.pdfbox.cos.COSFloat;
40 import org.pdfbox.exceptions.OutlineNotLocalException;
41 import org.pdfbox.pdmodel.PDDestinationNameTreeNode;
42 import org.pdfbox.pdmodel.PDDocument;
43 import org.pdfbox.pdmodel.PDDocumentNameDictionary;
44 import org.pdfbox.pdmodel.PDPage;
45 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement;
46 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceInstance;
47 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
48 import org.pdfbox.pdmodel.interactive.action.type.PDAction;
49 import org.pdfbox.pdmodel.interactive.action.type.PDActionGoTo;
50 import org.pdfbox.pdmodel.interactive.action.PDActionFactory;
51 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
52 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination;
53 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
54 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination;
55 import org.pdfbox.util.BitFlagHelper;
56
57 /**
58  * This represents an outline in a pdf document.
59  *
60  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
61  * @version $Revision: 1.7 $
62  */

63 public class PDOutlineItem extends PDOutlineNode
64 {
65     
66     private static final int ITALIC_FLAG = 1;
67     private static final int BOLD_FLAG = 2;
68     
69     /**
70      * Default Constructor.
71      */

72     public PDOutlineItem()
73     {
74         super();
75     }
76     
77     /**
78      * Constructor for an existing outline item.
79      *
80      * @param dic The storage dictionary.
81      */

82     public PDOutlineItem( COSDictionary dic )
83     {
84         super( dic );
85     }
86     
87     /**
88      * Insert a sibling after this node.
89      *
90      * @param item The item to insert.
91      */

92     public void insertSiblingAfter( PDOutlineItem item )
93     {
94         item.setParent( getParent() );
95         PDOutlineItem next = getNextSibling();
96         setNextSibling( item );
97         item.setPreviousSibling( this );
98         if( next != null )
99         {
100             item.setNextSibling( next );
101             next.setPreviousSibling( item );
102         }
103         updateParentOpenCount( 1 );
104     }
105     
106     /**
107      * Return the previous sibling or null if there is no sibling.
108      *
109      * @return The previous sibling.
110      */

111     public PDOutlineItem getPreviousSibling()
112     {
113         PDOutlineItem last = null;
114         COSDictionary lastDic = (COSDictionary)node.getDictionaryObject( "Prev" );
115         if( lastDic != null )
116         {
117             last = new PDOutlineItem( lastDic );
118         }
119         return last;
120     }
121     
122     /**
123      * Set the previous sibling, this will be maintained by this class.
124      *
125      * @param outlineNode The new previous sibling.
126      */

127     protected void setPreviousSibling( PDOutlineNode outlineNode )
128     {
129         node.setItem( "Prev", outlineNode );
130     }
131     
132     /**
133      * Return the next sibling or null if there is no next sibling.
134      *
135      * @return The next sibling.
136      */

137     public PDOutlineItem getNextSibling()
138     {
139         PDOutlineItem last = null;
140         COSDictionary lastDic = (COSDictionary)node.getDictionaryObject( "Next" );
141         if( lastDic != null )
142         {
143             last = new PDOutlineItem( lastDic );
144         }
145         return last;
146     }
147     
148     /**
149      * Set the next sibling, this will be maintained by this class.
150      *
151      * @param outlineNode The new next sibling.
152      */

153     protected void setNextSibling( PDOutlineNode outlineNode )
154     {
155         node.setItem( "Next", outlineNode );
156     }
157     
158     /**
159      * Get the title of this node.
160      *
161      * @return The title of this node.
162      */

163     public String JavaDoc getTitle()
164     {
165         return node.getString( "Title" );
166     }
167     
168     /**
169      * Set the title for this node.
170      *
171      * @param title The new title for this node.
172      */

173     public void setTitle( String JavaDoc title )
174     {
175         node.setString( "Title", title );
176     }
177     
178     /**
179      * Get the page destination of this node.
180      *
181      * @return The page destination of this node.
182      * @throws IOException If there is an error creating the destination.
183      */

184     public PDDestination getDestination() throws IOException JavaDoc
185     {
186         return PDDestination.create( node.getDictionaryObject( "Dest" ) );
187     }
188     
189     /**
190      * Set the page destination for this node.
191      *
192      * @param dest The new page destination for this node.
193      */

194     public void setDestination( PDDestination dest )
195     {
196         node.setItem( "Dest", dest );
197     }
198     
199     /**
200      * A convenience method that will create an XYZ destination using only the defaults.
201      *
202      * @param page The page to refer to.
203      */

204     public void setDestination( PDPage page )
205     {
206         PDPageXYZDestination dest = null;
207         if( page != null )
208         {
209             dest = new PDPageXYZDestination();
210             dest.setPage( page );
211         }
212         setDestination( dest );
213     }
214     
215     /**
216      * This method will attempt to find the page in this PDF document that this outline points to.
217      * If the outline does not point to anything then this method will return null. If the outline
218      * is an action that is not a GoTo action then this methods will throw the OutlineNotLocationException
219      *
220      * @param doc The document to get the page from.
221      *
222      * @return The page that this outline will go to when activated or null if it does not point to anything.
223      * @throws IOException If there is an error when trying to find the page.
224      */

225     public PDPage findDestinationPage( PDDocument doc ) throws IOException JavaDoc
226     {
227         PDPage page = null;
228         PDDestination rawDest = getDestination();
229         if( rawDest == null )
230         {
231             PDAction outlineAction = getAction();
232             if( outlineAction instanceof PDActionGoTo )
233             {
234                 rawDest = ((PDActionGoTo)outlineAction).getDestination();
235             }
236             else if( outlineAction == null )
237             {
238                 //if the outline action is null then this outline does not refer
239
//to anything and we will just return null.
240
}
241             else
242             {
243                 throw new OutlineNotLocalException( "Error: Outline does not reference a local page." );
244             }
245         }
246         
247         PDPageDestination pageDest = null;
248         if( rawDest instanceof PDNamedDestination )
249         {
250             //if we have a named destination we need to lookup the PDPageDestination
251
PDNamedDestination namedDest = (PDNamedDestination)rawDest;
252             PDDocumentNameDictionary namesDict = doc.getDocumentCatalog().getNames();
253             if( namesDict != null )
254             {
255                 PDDestinationNameTreeNode destsTree = namesDict.getDests();
256                 if( destsTree != null )
257                 {
258                     pageDest = (PDPageDestination)destsTree.getValue( namedDest.getNamedDestination() );
259                 }
260             }
261         }
262         else if( rawDest instanceof PDPageDestination)
263         {
264             pageDest = (PDPageDestination) rawDest;
265         }
266         else if( rawDest == null )
267         {
268             //if the destination is null then we will simply return a null page.
269
}
270         else
271         {
272             throw new IOException JavaDoc( "Error: Unknown destination type " + rawDest );
273         }
274         
275         if( pageDest != null )
276         {
277             page = pageDest.getPage();
278             if( page == null )
279             {
280                 int pageNumber = pageDest.getPageNumber();
281                 if( pageNumber != -1 )
282                 {
283                     List JavaDoc allPages = doc.getDocumentCatalog().getAllPages();
284                     page = (PDPage)allPages.get( pageNumber );
285                 }
286             }
287         }
288         
289         return page;
290     }
291     
292     /**
293      * Get the action of this node.
294      *
295      * @return The action of this node.
296      */

297     public PDAction getAction()
298     {
299         return PDActionFactory.createAction( (COSDictionary)node.getDictionaryObject( "A" ) );
300     }
301     
302     /**
303      * Set the action for this node.
304      *
305      * @param action The new action for this node.
306      */

307     public void setAction( PDAction action )
308     {
309         node.setItem( "A", action );
310     }
311     
312     /**
313      * Get the structure element of this node.
314      *
315      * @return The structure element of this node.
316      */

317     public PDStructureElement getStructureElement()
318     {
319         PDStructureElement se = null;
320         COSDictionary dic = (COSDictionary)node.getDictionaryObject( "SE" );
321         if( dic != null )
322         {
323             se = new PDStructureElement( dic );
324         }
325         return se;
326     }
327     
328     /**
329      * Set the structure element for this node.
330      *
331      * @param structureElement The new structure element for this node.
332      */

333     public void setStructuredElement( PDStructureElement structureElement )
334     {
335         node.setItem( "SE", structureElement );
336     }
337     
338     /**
339      * Get the text color of this node. Default is black and this method
340      * will never return null.
341      *
342      * @return The structure element of this node.
343      */

344     public PDColorSpaceInstance getTextColor()
345     {
346         PDColorSpaceInstance retval = null;
347         COSArray csValues = (COSArray)node.getDictionaryObject( "C" );
348         if( csValues == null )
349         {
350             csValues = new COSArray();
351             csValues.growToSize( 3, new COSFloat( 0 ) );
352             node.setItem( "C", csValues );
353         }
354         retval = new PDColorSpaceInstance(csValues);
355         retval.setColorSpace( PDDeviceRGB.INSTANCE );
356         return retval;
357     }
358     
359     /**
360      * Set the text color for this node. The colorspace must be a PDDeviceRGB.
361      *
362      * @param textColor The text color for this node.
363      */

364     public void setTextColor( PDColorSpaceInstance textColor )
365     {
366         node.setItem( "C", textColor.getCOSColorSpaceValue() );
367     }
368     
369     /**
370      * Set the text color for this node. The colorspace must be a PDDeviceRGB.
371      *
372      * @param textColor The text color for this node.
373      */

374     public void setTextColor( Color JavaDoc textColor )
375     {
376         COSArray array = new COSArray();
377         array.add( new COSFloat( textColor.getRed()/255f));
378         array.add( new COSFloat( textColor.getGreen()/255f));
379         array.add( new COSFloat( textColor.getBlue()/255f));
380         node.setItem( "C", array );
381     }
382     
383     /**
384      * A flag telling if the text should be italic.
385      *
386      * @return The italic flag.
387      */

388     public boolean isItalic()
389     {
390         return BitFlagHelper.getFlag( node, "F", ITALIC_FLAG );
391     }
392     
393     /**
394      * Set the italic property of the text.
395      *
396      * @param italic The new italic flag.
397      */

398     public void setItalic( boolean italic )
399     {
400         BitFlagHelper.setFlag( node, "F", ITALIC_FLAG, italic );
401     }
402     
403     /**
404      * A flag telling if the text should be bold.
405      *
406      * @return The bold flag.
407      */

408     public boolean isBold()
409     {
410         return BitFlagHelper.getFlag( node, "F", BOLD_FLAG );
411     }
412     
413     /**
414      * Set the bold property of the text.
415      *
416      * @param bold The new bold flag.
417      */

418     public void setBold( boolean bold )
419     {
420         BitFlagHelper.setFlag( node, "F", BOLD_FLAG, bold );
421     }
422      
423 }
424
Popular Tags