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.pdmodel.PDDocument;
033: import org.pdfbox.pdmodel.PDPage;
034: import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
035: import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
036: import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
037:
038: import java.util.List;
039:
040: /**
041: * This is an example on how to add bookmarks to a PDF document. It simply
042: * adds 1 bookmark for every page.
043: *
044: * Usage: java org.pdfbox.examples.pdmodel.CreateBookmarks <input-pdf> <output-pdf>
045: *
046: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
047: * @version $Revision: 1.2 $
048: */
049: public class CreateBookmarks {
050: private CreateBookmarks() {
051: //utility class
052: }
053:
054: /**
055: * This will print the documents data.
056: *
057: * @param args The command line arguments.
058: *
059: * @throws Exception If there is an error parsing the document.
060: */
061: public static void main(String[] args) throws Exception {
062: if (args.length != 2) {
063: usage();
064: } else {
065: PDDocument document = null;
066: try {
067: document = PDDocument.load(args[0]);
068: if (document.isEncrypted()) {
069: System.err
070: .println("Error: Cannot add bookmarks to encrypted document.");
071: System.exit(1);
072: }
073: PDDocumentOutline outline = new PDDocumentOutline();
074: document.getDocumentCatalog().setDocumentOutline(
075: outline);
076: PDOutlineItem pagesOutline = new PDOutlineItem();
077: pagesOutline.setTitle("All Pages");
078: outline.appendChild(pagesOutline);
079: List pages = document.getDocumentCatalog()
080: .getAllPages();
081: for (int i = 0; i < pages.size(); i++) {
082: PDPage page = (PDPage) pages.get(i);
083: PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
084: dest.setPage(page);
085: PDOutlineItem bookmark = new PDOutlineItem();
086: bookmark.setDestination(dest);
087: bookmark.setTitle("Page " + (i + 1));
088: pagesOutline.appendChild(bookmark);
089: }
090: pagesOutline.openNode();
091: outline.openNode();
092:
093: document.save(args[1]);
094: } finally {
095: if (document != null) {
096: document.close();
097: }
098: }
099: }
100: }
101:
102: /**
103: * This will print the usage for this document.
104: */
105: private static void usage() {
106: System.err
107: .println("Usage: java org.pdfbox.examples.pdmodel.CreateBookmarks <input-pdf> <output-pdf>");
108: }
109: }
|