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 ConvertMultipleUrlsToPdf
        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
                ' read parameters from the webpage
                Dim url1 As String = TxtUrl1.Text
                Dim url2 As String = TxtUrl2.Text
                Dim url3 As String = TxtUrl3.Text

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

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

                ' add the first url
                Dim html1 As New PdfHtmlElement(url1)
                Dim result As PdfRenderingResult = page.Add(html1)

                ' get the last page of the previous conversion
                Dim page2 As PdfPage = doc.Pages(result.PdfPageLastIndex)

                ' add the second url
                Dim html2 As New PdfHtmlElement(result.PdfPageLastRectangle.Left, _
                                                result.PdfPageLastRectangle.Bottom, url2)
                result = page2.Add(html2)

                ' get the last page of the previous conversion
                Dim page3 As PdfPage = doc.Pages(result.PdfPageLastIndex)

                ' add the third url
                Dim html3 As New PdfHtmlElement(result.PdfPageLastRectangle.Left, _
                                                result.PdfPageLastRectangle.Bottom, url3)
                result = page3.Add(html3)

                ' 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 = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + _
                                                      "/../../" + "ConvertMultipleUrlsToPdf.vb")
                WBSampleCode.DocumentText = New CodeColorizer().Colorize(code, Languages.VbDotNet)
            Catch
                WBSampleCode.DocumentText = "Sample code not available"
            End Try
        End Sub

    End Class
End Namespace
