using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ColorCode;
using SelectPdf;

namespace SelectPdf.Samples
{
    public partial class PdfBookmarks : Form
    {
        private void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            string file = "Document.pdf";

            try
            {
                // create a new pdf document
                PdfDocument doc = 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
                PdfPage page1 = doc.AddPage();

                // add a second page to the document
                PdfPage page2 = doc.AddPage();

                // add a third page to the document
                PdfPage page3 = doc.AddPage();

                // define a rendering result object
                PdfRenderingResult result;

                // create a new pdf font
                PdfFont font = doc.AddFont(PdfStandardFont.Helvetica);
                font.Size = 14;

                // create a new text element and add it to the first page
                PdfTextElement text1 = new PdfTextElement(0, 0, "First page", font);
                page1.Add(text1);

                // create a new text element and add it to the second page
                PdfTextElement text2 = new PdfTextElement(0, 0, "Second page", font);
                page2.Add(text2);

                // create a new text element and add it to the third page
                PdfTextElement text3 = new PdfTextElement(0, 0, "Third page", font);
                page3.Add(text3);

                // create a first level bookmark pointing to the first page
                PdfBookmark book1 = doc.AddBookmark("First page", new PdfDestination(page1,
                    new System.Drawing.PointF(0, 0)));

                // create a first level bookmark pointing to the second page
                PdfBookmark book2 = doc.AddBookmark("Second page", new PdfDestination(page2,
                    new System.Drawing.PointF(0, 0)));

                // create a first level bookmark pointing to the third page
                PdfBookmark book3 = doc.AddBookmark("Third page", new PdfDestination(page3,
                    new System.Drawing.PointF(0, 0)));

                // other text on page 2
                PdfTextElement text21 = 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
                PdfTextElement text22 = 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
                PdfBookmark book = 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 (Exception ex)
            {
                MessageBox.Show(String.Format("Error: {0}", ex.Message),
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }

            // open generated pdf
            try
            {
                System.Diagnostics.Process.Start(file);
            }
            catch
            {
                MessageBox.Show("Could not open generated pdf document",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public PdfBookmarks()
        {
            InitializeComponent();
            try
            {
                // display highlighted sample code
                string code = Helper.ReadSourceCode("PdfBookmarks.cs");
                WBSampleCode.DocumentText = new CodeColorizer().Colorize(code, Languages.CSharp);
            }
            catch
            {
                WBSampleCode.DocumentText = "Sample code not available";
            }
        }

    }
}
