01: package it.stefanochizzolini.clown.samples;
02:
03: import it.stefanochizzolini.clown.documents.Document;
04: import it.stefanochizzolini.clown.documents.Page;
05: import it.stefanochizzolini.clown.documents.interaction.navigation.document.Bookmark;
06: import it.stefanochizzolini.clown.documents.interaction.navigation.document.Bookmarks;
07: import it.stefanochizzolini.clown.files.File;
08: import it.stefanochizzolini.clown.objects.PdfReference;
09: import it.stefanochizzolini.clown.tokens.FileFormatException;
10:
11: /**
12: This sample demonstrates how to inspect the bookmarks of a PDF document.
13: */
14: public class BookmarksParsingSample implements ISample {
15: public void run(PDFClownSampleLoader loader) {
16: // (boilerplate user choice -- ignore it)
17: String filePath = loader
18: .getPdfFileChoice("Please select a PDF file");
19:
20: // 1. Open the PDF file!
21: File file;
22: try {
23: file = new File(filePath);
24: } catch (FileFormatException e) {
25: throw new RuntimeException(filePath
26: + " file has a bad file format.", e);
27: } catch (Exception e) {
28: throw new RuntimeException(
29: filePath + " file access error.", e);
30: }
31:
32: // Get the PDF document!
33: Document document = file.getDocument();
34:
35: // 2. Get the bookmarks collection!
36: Bookmarks bookmarks = document.getBookmarks();
37: if (bookmarks == null) {
38: System.out
39: .println("\nNo bookmark available (Outline dictionary doesn't exist).");
40: return;
41: }
42:
43: System.out
44: .println("\nIterating through the bookmarks collection (please wait)...\n");
45:
46: // 3. Show the bookmarks!
47: printBookmarks(bookmarks);
48: }
49:
50: private void printBookmarks(Bookmarks bookmarks) {
51: if (bookmarks == null)
52: return;
53:
54: for (Bookmark bookmark : bookmarks) {
55: // Show current bookmark!
56: Page page = bookmark.getDestination().getPage();
57: System.out
58: .println("Bookmark '" + bookmark.getTitle() + "'");
59: System.out.println(" Target Page: number = "
60: + (page.getIndex() + 1) + "; ID = "
61: + ((PdfReference) page.getBaseObject()).getID());
62:
63: // Show child bookmarks!
64: printBookmarks(bookmark.getBookmarks());
65: }
66: }
67: }
|