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 PdfImageElementForm
        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()

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

                ' get image path
                Dim imgFile As String = AppDomain.CurrentDomain.BaseDirectory + _
                    "/../../files/logo.png"

                ' define a rendering result object
                Dim result As PdfRenderingResult

                ' create image element from file path with real image size
                Dim img1 As New PdfImageElement(0, 0, imgFile)
                result = page.Add(img1)

                ' create image element from system Image object with fixed size in pdf 
                ' (aspect ratio not preserved)
                Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(imgFile)
                Dim img2 As New PdfImageElement(0, _
                        result.PdfPageLastRectangle.Bottom + 50, 100, 100, img)
                result = page.Add(img2)

                ' add text element
                Dim font1 As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
                font1.Size = 10
                Dim text1 As New PdfTextElement(0, _
                        result.PdfPageLastRectangle.Bottom + 50, Helper.SomeText(), font1)
                page.Add(text1)

                ' add image over text (no transparency)
                Dim img3 As New PdfImageElement(0, _
                        result.PdfPageLastRectangle.Bottom + 50, imgFile)
                result = page.Add(img3)

                ' add image over text (with transparency) next to the previous image
                Dim img4 As New PdfImageElement( _
                    result.PdfPageLastRectangle.Right + 30, _
                    result.PdfPageLastRectangle.Top, imgFile)
                img4.Transparency = 50
                result = page.Add(img4)

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

    End Class
End Namespace
