Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Reflection
Imports C1.Silverlight.PdfViewer
Imports C1.Silverlight

Namespace PdfViewerSamples
    Partial Public Class BookmarksDemo
        Inherits UserControl
        Public Sub New()
            InitializeComponent()
          
            Dim resource = Application.GetResourceStream(New Uri("/" + New AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Bookmarks.pdf", UriKind.Relative))
            pdf.LoadDocument(resource.Stream)
        End Sub

        Private Sub C1TreeView_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
            If e.AddedItems.Count = 0 Then
                Return
            End If
            Dim treeViewItem = TryCast(e.AddedItems(0), C1TreeViewItem)
            If treeViewItem Is Nothing Then
                Return
            End If
            Dim bookmark = TryCast(treeViewItem.Header, Bookmark)
            If bookmark Is Nothing Then
                Return
            End If

            Dim title As New List(Of String)

            While Not IsNothing(bookmark)
                title.Insert(0, bookmark.Title)
                If IsNothing(treeViewItem.ParentItem) Then
                    Exit While
                End If
                treeViewItem = TryCast(treeViewItem.ParentItem, C1TreeViewItem)
                If IsNothing(treeViewItem) Then
                    Exit While
                End If
                bookmark = TryCast(treeViewItem.Header, Bookmark)
                If (IsNothing(bookmark)) Then
                    Exit While
                End If
            End While
            pdf.GoToBookmark(title.ToArray())
        End Sub
    End Class
End Namespace
