001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import org.pdfbox.exceptions.InvalidPasswordException;
033:
034: import org.pdfbox.pdfparser.PDFParser;
035:
036: import org.pdfbox.pdmodel.PDDocument;
037: import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
038: import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
039: import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
040:
041: import java.io.FileInputStream;
042: import java.io.IOException;
043:
044: /**
045: * This is an example on how to access the bookmarks that are part of a pdf document.
046: *
047: * Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf>
048: *
049: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
050: * @version $Revision: 1.2 $
051: */
052: public class PrintBookmarks {
053: /**
054: * This will print the documents data.
055: *
056: * @param args The command line arguments.
057: *
058: * @throws Exception If there is an error parsing the document.
059: */
060: public static void main(String[] args) throws Exception {
061: if (args.length != 1) {
062: usage();
063: } else {
064: PDDocument document = null;
065: FileInputStream file = null;
066: try {
067: file = new FileInputStream(args[0]);
068: PDFParser parser = new PDFParser(file);
069: parser.parse();
070: document = parser.getPDDocument();
071: if (document.isEncrypted()) {
072: try {
073: document.decrypt("");
074: } catch (InvalidPasswordException e) {
075: System.err
076: .println("Error: Document is encrypted with a password.");
077: System.exit(1);
078: }
079: }
080: PrintBookmarks meta = new PrintBookmarks();
081: PDDocumentOutline outline = document
082: .getDocumentCatalog().getDocumentOutline();
083: if (outline != null) {
084: meta.printBookmark(outline, "");
085: } else {
086: System.out
087: .println("This document does not contain any bookmarks");
088: }
089: } finally {
090: if (file != null) {
091: file.close();
092: }
093: if (document != null) {
094: document.close();
095: }
096: }
097: }
098: }
099:
100: /**
101: * This will print the usage for this document.
102: */
103: private static void usage() {
104: System.err
105: .println("Usage: java org.pdfbox.examples.pdmodel.PrintBookmarks <input-pdf>");
106: }
107:
108: /**
109: * This will print the documents bookmarks to System.out.
110: *
111: * @param bookmark The bookmark to print out.
112: * @param indentation A pretty printing parameter
113: *
114: * @throws IOException If there is an error getting the page count.
115: */
116: public void printBookmark(PDOutlineNode bookmark, String indentation)
117: throws IOException {
118: PDOutlineItem current = bookmark.getFirstChild();
119: while (current != null) {
120: System.out.println(indentation + current.getTitle());
121: printBookmark(current, indentation + " ");
122: current = current.getNextSibling();
123: }
124:
125: }
126: }
|