forked from Patagames/Pdf.WinForms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookmarksViewer.cs
165 lines (146 loc) · 4.1 KB
/
BookmarksViewer.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Patagames.Pdf.Enums;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
namespace Patagames.Pdf.Net.Controls.WinForms
{
/// <summary>
/// Represents the BookmarksViewer control for displaying bookmarks contained in PDF document.
/// </summary>
public partial class BookmarksViewer : TreeView
{
#region Private fields
private PdfViewer _pdfViewer = null;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets PdfViewer control associated with this BookmarkViewer control
/// </summary>
public PdfViewer PdfViewer
{
get
{
return _pdfViewer;
}
set
{
if (_pdfViewer != value)
OnPdfViewerChanging(_pdfViewer, value);
}
}
#endregion
#region Constructors and initialization
/// <summary>
/// Initializes a new instance of the <see cref="BookmarksViewer"/> class.
/// </summary>
public BookmarksViewer()
{
InitializeComponent();
}
/// <summary>
/// Initializes a new instance of the <see cref="BookmarksViewer"/> class.
/// </summary>
/// <param name="container">Container</param>
public BookmarksViewer(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#endregion
#region Overrides
/// <summary>
/// Raises the System.Windows.Forms.TreeView.AfterSelect event.
/// </summary>
/// <param name="e">A System.Windows.Forms.TreeViewEventArgs that contains the event data.</param>
protected override void OnAfterSelect(TreeViewEventArgs e)
{
var node = e.Node as BookmarksViewerNode;
if (node == null || node.Bookmark == null)
return;
if (node.Bookmark.Action != null)
ProcessAction(node.Bookmark.Action);
else if (node.Bookmark.Destination != null)
ProcessDestination(node.Bookmark.Destination);
base.OnAfterSelect(e);
}
#endregion
#region Protected methods
/// <summary>
/// Called when the current PdfViewer control associated with the ToolStrip is changing.
/// </summary>
/// <param name="oldValue">PdfViewer control of which was associated with the ToolStrip.</param>
/// <param name="newValue">PdfViewer control of which will be associated with the ToolStrip.</param>
protected virtual void OnPdfViewerChanging(PdfViewer oldValue, PdfViewer newValue)
{
if (oldValue != null)
{
oldValue.AfterDocumentChanged -= pdfViewer_DocumentChanged;
oldValue.DocumentClosed -= pdfViewer_DocumentClosed;
oldValue.DocumentLoaded -= pdfViewer_DocumentLoaded;
}
if (newValue != null)
{
newValue.AfterDocumentChanged += pdfViewer_DocumentChanged;
newValue.DocumentClosed += pdfViewer_DocumentClosed;
newValue.DocumentLoaded += pdfViewer_DocumentLoaded;
}
_pdfViewer = newValue;
RebuildTree();
}
#endregion
#region Private event handlers
private void pdfViewer_DocumentChanged(object sender, EventArgs e)
{
RebuildTree();
}
private void pdfViewer_DocumentLoaded(object sender, EventArgs e)
{
RebuildTree();
}
private void pdfViewer_DocumentClosed(object sender, EventArgs e)
{
RebuildTree();
}
#endregion
#region Private methods
private void BuildTree(TreeNodeCollection nodes, PdfBookmarkCollections bookmarks)
{
if (bookmarks == null)
return;
foreach (var b in bookmarks)
{
var node = new BookmarksViewerNode(b);
nodes.Add(node);
if (b.Childs != null && b.Childs.Count > 0)
BuildTree(node.Nodes, b.Childs);
}
}
private void ProcessAction(PdfAction pdfAction)
{
if (pdfAction.ActionType == ActionTypes.Uri)
Process.Start(pdfAction.ActionUrl);
else if (pdfAction.Destination != null)
ProcessDestination(pdfAction.Destination);
}
private void ProcessDestination(PdfDestination pdfDestination)
{
if (_pdfViewer == null)
return;
_pdfViewer.ScrollToPage(pdfDestination.PageIndex);
_pdfViewer.Invalidate();
}
#endregion
#region Public methods
/// <summary>
/// Constructs the tree of bookmarks
/// </summary>
public void RebuildTree()
{
Nodes.Clear();
if (_pdfViewer != null && _pdfViewer.Document != null)
BuildTree(Nodes, _pdfViewer.Document.Bookmarks);
}
#endregion
}
}