KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > PDDocumentCatalog


1 /**
2  * Copyright (c) 2003-2006, 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;
32
33 import java.io.IOException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.pdfbox.cos.COSArray;
38 import org.pdfbox.cos.COSBase;
39 import org.pdfbox.cos.COSDictionary;
40 import org.pdfbox.cos.COSName;
41 import org.pdfbox.cos.COSStream;
42 import org.pdfbox.cos.COSString;
43
44 import org.pdfbox.pdmodel.common.COSArrayList;
45 import org.pdfbox.pdmodel.common.COSObjectable;
46 import org.pdfbox.pdmodel.common.PDDestinationOrAction;
47 import org.pdfbox.pdmodel.common.PDMetadata;
48 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo;
49 import org.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot;
50 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI;
51 import org.pdfbox.pdmodel.interactive.action.PDActionFactory;
52 import org.pdfbox.pdmodel.interactive.action.PDDocumentCatalogAdditionalActions;
53 import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination;
54 import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
55 import org.pdfbox.pdmodel.interactive.form.PDAcroForm;
56
57 import org.pdfbox.pdmodel.interactive.pagenavigation.PDThread;
58 import org.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences;
59
60 /**
61  * This class represents the acroform of a PDF document.
62  *
63  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
64  * @version $Revision: 1.21 $
65  */

66 public class PDDocumentCatalog implements COSObjectable
67 {
68     private COSDictionary root;
69     private PDDocument document;
70
71     private PDAcroForm acroForm = null;
72     
73     /**
74      * Page mode where neither the outline nor the thumbnails
75      * are displayed.
76      */

77     public static final String JavaDoc PAGE_MODE_USE_NONE = "UseNone";
78     /**
79      * Show bookmarks when pdf is opened.
80      */

81     public static final String JavaDoc PAGE_MODE_USE_OUTLINES = "UseOutlines";
82     /**
83      * Show thumbnails when pdf is opened.
84      */

85     public static final String JavaDoc PAGE_MODE_USE_THUMBS = "UseThumbs";
86     /**
87      * Full screen mode with no menu bar, window controls.
88      */

89     public static final String JavaDoc PAGE_MODE_FULL_SCREEN = "FullScreen";
90     /**
91      * Optional content group panel is visible when opened.
92      */

93     public static final String JavaDoc PAGE_MODE_USE_OPTIONAL_CONTENT = "UseOC";
94     /**
95      * Attachments panel is visible.
96      */

97     public static final String JavaDoc PAGE_MODE_USE_ATTACHMENTS = "UseAttachments";
98     
99     /**
100      * Display one page at a time.
101      */

102     public static final String JavaDoc PAGE_LAYOUT_SINGLE_PAGE = "SinglePage";
103     /**
104      * Display the pages in one column.
105      */

106     public static final String JavaDoc PAGE_LAYOUT_ONE_COLUMN = "OneColumn";
107     /**
108      * Display the pages in two columns, with odd numbered pagse on the left.
109      */

110     public static final String JavaDoc PAGE_LAYOUT_TWO_COLUMN_LEFT = "TwoColumnLeft";
111     /**
112      * Display the pages in two columns, with odd numbered pagse on the right.
113      */

114     public static final String JavaDoc PAGE_LAYOUT_TWO_COLUMN_RIGHT ="TwoColumnRight";
115     /**
116      * Display the pages two at a time, with odd-numbered pages on the left.
117      * @since PDF Version 1.5
118      */

119     public static final String JavaDoc PAGE_LAYOUT_TWO_PAGE_LEFT = "TwoPageLeft";
120     /**
121      * Display the pages two at a time, with odd-numbered pages on the right.
122      * @since PDF Version 1.5
123      */

124     public static final String JavaDoc PAGE_LAYOUT_TWO_PAGE_RIGHT = "TwoPageRight";
125     
126     
127     
128     /**
129      * Constructor.
130      *
131      * @param doc The document that this catalog is part of.
132      */

133     public PDDocumentCatalog( PDDocument doc )
134     {
135         document = doc;
136         root = new COSDictionary();
137         root.setItem( COSName.TYPE, new COSString( "Catalog" ) );
138         document.getDocument().getTrailer().setItem( COSName.ROOT, root );
139     }
140
141     /**
142      * Constructor.
143      *
144      * @param doc The document that this catalog is part of.
145      * @param rootDictionary The root dictionary that this object wraps.
146      */

147     public PDDocumentCatalog( PDDocument doc, COSDictionary rootDictionary )
148     {
149         document = doc;
150         root = rootDictionary;
151     }
152     
153     /**
154      * Convert this standard java object to a COS object.
155      *
156      * @return The cos object that matches this Java object.
157      */

158     public COSBase getCOSObject()
159     {
160         return root;
161     }
162
163     /**
164      * Convert this standard java object to a COS object.
165      *
166      * @return The cos object that matches this Java object.
167      */

168     public COSDictionary getCOSDictionary()
169     {
170         return root;
171     }
172
173     /**
174      * This will get the documents acroform. This will return null if
175      * no acroform is part of the document.
176      *
177      * @return The documents acroform.
178      */

179     public PDAcroForm getAcroForm()
180     {
181         if( acroForm == null )
182         {
183             COSDictionary acroFormDic =
184                 (COSDictionary)root.getDictionaryObject( COSName.ACRO_FORM );
185             if( acroFormDic != null )
186             {
187                 acroForm = new PDAcroForm( document, acroFormDic );
188             }
189         }
190         return acroForm;
191     }
192     
193     /**
194      * Set the acro form for this catalog.
195      *
196      * @param acro The new acro form.
197      */

198     public void setAcroForm( PDAcroForm acro )
199     {
200         root.setItem( COSName.ACRO_FORM, acro );
201     }
202
203     /**
204      * This will get the root node for the pages.
205      *
206      * @return The parent page node.
207      */

208     public PDPageNode getPages()
209     {
210         return new PDPageNode( (COSDictionary)root.getDictionaryObject( COSName.PAGES ) );
211     }
212
213     /**
214      * The PDF document contains a hierarchical structure of PDPageNode and PDPages, which
215      * is mostly just a way to store this information. This method will return a flat list
216      * of all PDPage objects in this document.
217      *
218      * @return A list of PDPage objects.
219      */

220     public List JavaDoc getAllPages()
221     {
222         List JavaDoc retval = new ArrayList JavaDoc();
223         PDPageNode rootNode = getPages();
224         //old (slower):
225
//getPageObjects( rootNode, retval );
226
rootNode.getAllKids(retval);
227         return retval;
228     }
229     
230     /**
231      * Get the viewer preferences associated with this document or null if they
232      * do not exist.
233      *
234      * @return The document's viewer preferences.
235      */

236     public PDViewerPreferences getViewerPreferences()
237     {
238         PDViewerPreferences retval = null;
239         COSDictionary dict = (COSDictionary)root.getDictionaryObject( "ViewerPreferences" );
240         if( dict != null )
241         {
242             retval = new PDViewerPreferences( dict );
243         }
244         
245         return retval;
246     }
247     
248     /**
249      * Set the viewer preferences.
250      *
251      * @param prefs The new viewer preferences.
252      */

253     public void setViewerPreferences( PDViewerPreferences prefs )
254     {
255         root.setItem( "ViewerPreferences", prefs );
256     }
257     
258     /**
259      * Get the outline associated with this document or null if it
260      * does not exist.
261      *
262      * @return The document's outline.
263      */

264     public PDDocumentOutline getDocumentOutline()
265     {
266         PDDocumentOutline retval = null;
267         COSDictionary dict = (COSDictionary)root.getDictionaryObject( "Outlines" );
268         if( dict != null )
269         {
270             retval = new PDDocumentOutline( dict );
271         }
272         
273         return retval;
274     }
275     
276     /**
277      * Set the document outlines.
278      *
279      * @param outlines The new document outlines.
280      */

281     public void setDocumentOutline( PDDocumentOutline outlines )
282     {
283         root.setItem( "Outlines", outlines );
284     }
285     
286     /**
287      * Get the list threads for this pdf document.
288      *
289      * @return A list of PDThread objects.
290      */

291     public List JavaDoc getThreads()
292     {
293         COSArray array = (COSArray)root.getDictionaryObject( "Threads" );
294         if( array == null )
295         {
296             array = new COSArray();
297             root.setItem( "Threads", array );
298         }
299         List JavaDoc pdObjects = new ArrayList JavaDoc();
300         for( int i=0; i<array.size(); i++ )
301         {
302             pdObjects.add( new PDThread( (COSDictionary)array.getObject( i ) ) );
303         }
304         return new COSArrayList( pdObjects, array );
305     }
306     
307     /**
308      * Set the list of threads for this pdf document.
309      *
310      * @param threads The list of threads, or null to clear it.
311      */

312     public void setThreads( List JavaDoc threads )
313     {
314         root.setItem( "Threads", COSArrayList.converterToCOSArray( threads ) );
315     }
316     
317     /**
318      * Get the metadata that is part of the document catalog. This will
319      * return null if there is no meta data for this object.
320      *
321      * @return The metadata for this object.
322      */

323     public PDMetadata getMetadata()
324     {
325         PDMetadata retval = null;
326         COSStream stream = (COSStream)root.getDictionaryObject( "Metadata" );
327         if( stream != null )
328         {
329             retval = new PDMetadata( stream );
330         }
331         return retval;
332     }
333     
334     /**
335      * Set the metadata for this object. This can be null.
336      *
337      * @param meta The meta data for this object.
338      */

339     public void setMetadata( PDMetadata meta )
340     {
341         root.setItem( "Metadata", meta );
342     }
343     
344     /**
345      * Set the Document Open Action for this object.
346      *
347      * @param action The action you want to perform.
348      */

349     public void setOpenAction( PDDestinationOrAction action )
350     {
351         root.setItem( COSName.getPDFName("OpenAction"), action );
352     }
353
354     /**
355      * Get the Document Open Action for this object.
356      *
357      * @return The action to perform when the document is opened.
358      *
359      * @throws IOException If there is an error creating the destination
360      * or action.
361      */

362     public PDDestinationOrAction getOpenAction() throws IOException JavaDoc
363     {
364         PDDestinationOrAction action = null;
365         COSBase actionObj = root.getDictionaryObject("OpenAction");
366         
367         if( actionObj == null )
368         {
369             //no op
370
}
371         else if( actionObj instanceof COSDictionary )
372         {
373             action = PDActionFactory.createAction((COSDictionary)actionObj);
374         }
375         else if( actionObj instanceof COSArray )
376         {
377             action = PDDestination.create( actionObj );
378         }
379         else
380         {
381             throw new IOException JavaDoc( "Unknown OpenAction " + actionObj );
382         }
383         
384         return action;
385     }
386     /**
387      * @return The Additional Actions for this Document
388      */

389     public PDDocumentCatalogAdditionalActions getActions()
390     {
391         COSDictionary addAct = (COSDictionary) root.getDictionaryObject("AA");
392         if (addAct == null)
393         {
394             addAct = new COSDictionary();
395             root.setItem("AA", addAct);
396         }
397         return new PDDocumentCatalogAdditionalActions(addAct);
398     }
399     
400     /**
401      * Set the additional actions for the document.
402      *
403      * @param actions The actions that are associated with this document.
404      */

405     public void setActions( PDDocumentCatalogAdditionalActions actions )
406     {
407         root.setItem("AA", actions );
408     }
409     
410     /**
411      * @return The names dictionary for this document or null if none exist.
412      */

413     public PDDocumentNameDictionary getNames()
414     {
415         PDDocumentNameDictionary nameDic = null;
416         COSDictionary names = (COSDictionary) root.getDictionaryObject("Names");
417         if(names != null)
418         {
419             nameDic = new PDDocumentNameDictionary(this,names);
420         }
421         return nameDic;
422     }
423     
424     /**
425      * Set the names dictionary for the document.
426      *
427      * @param names The names dictionary that is associated with this document.
428      */

429     public void setNames( PDDocumentNameDictionary names )
430     {
431         root.setItem("Names", names );
432     }
433     
434     /**
435      * Get info about doc's usage of tagged features. This will return
436      * null if there is no information.
437      *
438      * @return The new mark info.
439      */

440     public PDMarkInfo getMarkInfo()
441     {
442         PDMarkInfo retval = null;
443         COSDictionary dic = (COSDictionary)root.getDictionaryObject( "MarkInfo" );
444         if( dic != null )
445         {
446             retval = new PDMarkInfo( dic );
447         }
448         return retval;
449     }
450     
451     /**
452      * Set information about the doc's usage of tagged features.
453      *
454      * @param markInfo The new MarkInfo data.
455      */

456     public void setMarkInfo( PDMarkInfo markInfo )
457     {
458         root.setItem( "MarkInfo", markInfo );
459     }
460     
461     /**
462      * Set the page display mode, see the PAGE_MODE_XXX constants.
463      * @return A string representing the page mode.
464      */

465     public String JavaDoc getPageMode()
466     {
467         return root.getNameAsString( "PageMode", PAGE_MODE_USE_NONE );
468     }
469     
470     /**
471      * Set the page mode. See the PAGE_MODE_XXX constants for valid values.
472      * @param mode The new page mode.
473      */

474     public void setPageMode( String JavaDoc mode )
475     {
476         root.setName( "PageMode", mode );
477     }
478     
479     /**
480      * Set the page layout, see the PAGE_LAYOUT_XXX constants.
481      * @return A string representing the page layout.
482      */

483     public String JavaDoc getPageLayout()
484     {
485         return root.getNameAsString( "PageLayout", PAGE_LAYOUT_SINGLE_PAGE );
486     }
487     
488     /**
489      * Set the page layout. See the PAGE_LAYOUT_XXX constants for valid values.
490      * @param layout The new page layout.
491      */

492     public void setPageLayout( String JavaDoc layout )
493     {
494         root.setName( "PageLayout", layout );
495     }
496     
497     /**
498      * Document level information in the URI.
499      * @return Document level URI.
500      */

501     public PDActionURI getURI()
502     {
503         PDActionURI retval = null;
504         COSDictionary uri = (COSDictionary)root.getDictionaryObject( "URI" );
505         if( uri != null )
506         {
507             retval = new PDActionURI( uri );
508         }
509         return retval;
510     }
511     
512     /**
513      * Set the document level uri.
514      * @param uri The new document level uri.
515      */

516     public void setURI( PDActionURI uri )
517     {
518         root.setItem( "URI", uri );
519     }
520     
521     /**
522      * Get the document's structure tree root.
523      *
524      * @return The document's structure tree root or null if none exists.
525      */

526     public PDStructureTreeRoot getStructureTreeRoot()
527     {
528         PDStructureTreeRoot treeRoot = null;
529         COSDictionary dic = (COSDictionary)root.getDictionaryObject( "StructTreeRoot" );
530         if( dic != null )
531         {
532             treeRoot = new PDStructureTreeRoot( dic );
533         }
534         return treeRoot;
535     }
536     
537     /**
538      * Set the document's structure tree root.
539      *
540      * @param treeRoot The new structure tree.
541      */

542     public void setStructureTreeRoot( PDStructureTreeRoot treeRoot )
543     {
544         root.setItem( "StructTreeRoot", treeRoot );
545     }
546     
547     /**
548      * The language for the document.
549      *
550      * @return The language for the document.
551      */

552     public String JavaDoc getLanguage()
553     {
554         return root.getString( "Lang" );
555     }
556     
557     /**
558      * Set the Language for the document.
559      *
560      * @param language The new document language.
561      */

562     public void setLanguage( String JavaDoc language )
563     {
564         root.setString( "Lang", language );
565     }
566 }
Popular Tags