KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > PrintBookmarks


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.examples.pdmodel;
32
33 import org.pdfbox.exceptions.InvalidPasswordException;
34
35 import org.pdfbox.pdfparser.PDFParser;
36
37 import org.pdfbox.pdmodel.PDDocument;
38 import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
39 import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
40 import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
41
42 import java.io.FileInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 /**
46  * This is an example on how to access the bookmarks that are part of a pdf document.
47  *
48  * Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf>
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.2 $
52  */

53 public class PrintBookmarks
54 {
55     /**
56      * This will print the documents data.
57      *
58      * @param args The command line arguments.
59      *
60      * @throws Exception If there is an error parsing the document.
61      */

62     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
63     {
64         if( args.length != 1 )
65         {
66             usage();
67         }
68         else
69         {
70             PDDocument document = null;
71             FileInputStream JavaDoc file = null;
72             try
73             {
74                 file = new FileInputStream JavaDoc( args[0] );
75                 PDFParser parser = new PDFParser( file );
76                 parser.parse();
77                 document = parser.getPDDocument();
78                 if( document.isEncrypted() )
79                 {
80                     try
81                     {
82                         document.decrypt( "" );
83                     }
84                     catch( InvalidPasswordException e )
85                     {
86                         System.err.println( "Error: Document is encrypted with a password." );
87                         System.exit( 1 );
88                     }
89                 }
90                 PrintBookmarks meta = new PrintBookmarks();
91                 PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline();
92                 if( outline != null )
93                 {
94                     meta.printBookmark( outline, "" );
95                 }
96                 else
97                 {
98                     System.out.println( "This document does not contain any bookmarks" );
99                 }
100             }
101             finally
102             {
103                 if( file != null )
104                 {
105                     file.close();
106                 }
107                 if( document != null )
108                 {
109                     document.close();
110                 }
111             }
112         }
113     }
114
115     /**
116      * This will print the usage for this document.
117      */

118     private static void usage()
119     {
120         System.err.println( "Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf>" );
121     }
122
123     /**
124      * This will print the documents bookmarks to System.out.
125      *
126      * @param bookmark The bookmark to print out.
127      * @param indentation A pretty printing parameter
128      *
129      * @throws IOException If there is an error getting the page count.
130      */

131     public void printBookmark( PDOutlineNode bookmark, String JavaDoc indentation ) throws IOException JavaDoc
132     {
133         PDOutlineItem current = bookmark.getFirstChild();
134         while( current != null )
135         {
136             System.out.println( indentation + current.getTitle() );
137             printBookmark( current, indentation + " " );
138             current = current.getNextSibling();
139         }
140         
141     }
142 }
Popular Tags