-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathBookmarksProvider.cs
145 lines (124 loc) · 5.01 KB
/
BookmarksProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
namespace UglyToad.PdfPig.Outline
{
using Actions;
using Content;
using Destinations;
using Logging;
using Parser.Parts;
using System.Collections.Generic;
using Core;
using Tokenization.Scanner;
using Tokens;
using Util;
internal class BookmarksProvider
{
private readonly ILog log;
private readonly IPdfTokenScanner pdfScanner;
public BookmarksProvider(ILog log, IPdfTokenScanner pdfScanner)
{
this.log = log;
this.pdfScanner = pdfScanner;
}
/// <summary>
/// Extract bookmarks, if any.
/// </summary>
public Bookmarks? GetBookmarks(Catalog catalog)
{
if (!catalog.CatalogDictionary.TryGet(NameToken.Outlines, pdfScanner, out DictionaryToken? outlinesDictionary))
{
return null;
}
if (outlinesDictionary.TryGet(NameToken.Type, pdfScanner, out NameToken? typeName) && typeName != NameToken.Outlines)
{
log?.Error($"Outlines (bookmarks) dictionary did not have correct type specified: {typeName}.");
}
if (!outlinesDictionary.TryGet(NameToken.First, pdfScanner, out DictionaryToken? next))
{
return null;
}
var roots = new List<BookmarkNode>();
var seen = new HashSet<IndirectReference>();
while (next != null)
{
ReadBookmarksRecursively(next, 0, false, seen, catalog.NamedDestinations, roots);
if (!next.TryGet(NameToken.Next, out IndirectReferenceToken nextReference)
|| !seen.Add(nextReference.Data))
{
break;
}
next = DirectObjectFinder.Get<DictionaryToken>(nextReference, pdfScanner);
}
return new Bookmarks(roots);
}
/// <summary>
/// Extract bookmarks recursively.
/// </summary>
private void ReadBookmarksRecursively(DictionaryToken nodeDictionary, int level, bool readSiblings, HashSet<IndirectReference> seen,
NamedDestinations namedDestinations,
List<BookmarkNode> list)
{
// 12.3 Document-Level Navigation
// 12.3.3 Document Outline - Title
// (Required) The text that shall be displayed on the screen for this item.
if (!nodeDictionary.TryGetOptionalStringDirect(NameToken.Title, pdfScanner, out var title))
{
throw new PdfDocumentFormatException($"Invalid title for outline (bookmark) node: {nodeDictionary}.");
}
var children = new List<BookmarkNode>();
if (nodeDictionary.TryGet(NameToken.First, pdfScanner, out DictionaryToken? firstChild))
{
ReadBookmarksRecursively(firstChild, level + 1, true, seen, namedDestinations, children);
}
BookmarkNode bookmark;
if (DestinationProvider.TryGetDestination(nodeDictionary, NameToken.Dest, namedDestinations, pdfScanner, log, false, out var destination))
{
bookmark = new DocumentBookmarkNode(title, level, destination, children);
}
else if (ActionProvider.TryGetAction(nodeDictionary, namedDestinations, pdfScanner, log, out var actionResult))
{
if (actionResult is GoToRAction goToRAction)
{
bookmark = new ExternalBookmarkNode(title, level, goToRAction.Destination, children, goToRAction.Filename);
}
else if (actionResult is GoToAction goToAction)
{
bookmark = new DocumentBookmarkNode(title, level, goToAction.Destination, children);
}
else if (actionResult is UriAction uriAction)
{
bookmark = new UriBookmarkNode(title, level, uriAction.Uri, children);
}
else
{
return;
}
}
else
{
log.Error($"No /Dest(ination) or /A(ction) entry found for bookmark node: {nodeDictionary}.");
return;
}
list.Add(bookmark);
if (!readSiblings)
{
return;
}
// Walk all siblings if this was the first child.
var current = nodeDictionary;
while (true)
{
if (!current.TryGet(NameToken.Next, out IndirectReferenceToken nextReference)
|| !seen.Add(nextReference.Data))
{
break;
}
current = DirectObjectFinder.Get<DictionaryToken>(nextReference, pdfScanner);
if (current is null)
{
break;
}
ReadBookmarksRecursively(current, level, false, seen, namedDestinations, list);
}
}
}
}