Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.IO
Imports ColorCode
Imports SelectPdf

Namespace SelectPdf.Samples
    Partial Public Class PdfBookmarks
        Inherits Form

        Private Sub BtnCreatePdf_Click(sender As Object, e As EventArgs) _
            Handles BtnCreatePdf.Click
            Cursor = Cursors.WaitCursor
            Dim file As String = "Document.pdf"

            Try
                ' create a new pdf document
                Dim doc As New PdfDocument()

                ' instruct the document to show the bookmarks panel when opened in a viewer
                doc.ViewerPreferences.PageMode = PdfViewerPageMode.UseOutlines

                ' add a new page to the document
                Dim page1 As PdfPage = doc.AddPage()

                ' add a second page to the document
                Dim page2 As PdfPage = doc.AddPage()

                ' add a third page to the document
                Dim page3 As PdfPage = doc.AddPage()

                ' define a rendering result object
                Dim result As PdfRenderingResult

                ' create a new pdf font
                Dim font As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
                font.Size = 14

                ' create a new text element and add it to the first page
                Dim text1 As New PdfTextElement(0, 0, "First page", font)
                page1.Add(text1)

                ' create a new text element and add it to the second page
                Dim text2 As New PdfTextElement(0, 0, "Second page", font)
                page2.Add(text2)

                ' create a new text element and add it to the third page
                Dim text3 As New PdfTextElement(0, 0, "Third page", font)
                page3.Add(text3)

                ' create a first level bookmark pointing to the first page
                Dim book1 As PdfBookmark = doc.AddBookmark("First page", _
                        New PdfDestination(page1, New System.Drawing.PointF(0, 0)))

                ' create a first level bookmark pointing to the second page
                Dim book2 As PdfBookmark = doc.AddBookmark("Second page", _
                        New PdfDestination(page2, New System.Drawing.PointF(0, 0)))

                ' create a first level bookmark pointing to the third page
                Dim book3 As PdfBookmark = doc.AddBookmark("Third page", _
                        New PdfDestination(page3, New System.Drawing.PointF(0, 0)))

                ' other text on page 2
                Dim text21 As New PdfTextElement(0, 100, "Text 1 on page 2", font)
                result = page2.Add(text21)

                ' add a level 2 bookmark to this text 
                ' (using the bookmark for the page 2 as parent and 
                ' the text rendering location as bookmark destination)
                doc.AddBookmark("Text 1", New PdfDestination(page2, _
                                result.PdfPageLastRectangle.Location), book2)

                ' other text on page 2
                Dim text22 As New PdfTextElement(0, 400, "Text 2 on page 2", font)
                result = page2.Add(text22)

                ' add a level 2 bookmark to this text 
                ' (using the bookmark for the page 2 as parent and the text rendering 
                ' location as bookmark destination) some styles are set for this bookmark
                Dim book As PdfBookmark = doc.AddBookmark("Text 2", _
                        New PdfDestination(page2, result.PdfPageLastRectangle.Location), book2)
                book.Color = System.Drawing.Color.Red
                book.Style = PdfBookmarkStyle.Italic

                ' save pdf document
                doc.Save(file)

                ' close pdf document
                doc.Close()
            Catch ex As Exception
                MessageBox.Show([String].Format("Error: {0}", ex.Message), "Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Return
            Finally
                Cursor = Cursors.Arrow
            End Try

            ' open generated pdf
            Try
                System.Diagnostics.Process.Start(file)
            Catch
                MessageBox.Show("Could not open generated pdf document", "Error", _
                                MessageBoxButtons.OK, MessageBoxIcon.[Error])
            End Try
        End Sub

        Public Sub New()
            InitializeComponent()
            Try
                ' display highlighted sample code
                Dim code As String = Helper.ReadSourceCode("PdfBookmarks.vb")
                WBSampleCode.DocumentText = New CodeColorizer().Colorize(code, Languages.VbDotNet)
            Catch
                WBSampleCode.DocumentText = "Sample code not available"
            End Try
        End Sub

    End Class
End Namespace
