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

                ' create a new pdf font (component standard font)
                Dim font1 As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
                font1.Size = 20

                ' create a new pdf font (system font)
                Dim font2 As PdfFont = doc.AddFont(New System.Drawing.Font("Verdana", 15))

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

                ' define a rendering result object
                Dim result As PdfRenderingResult

                ' create text element 
                Dim text1 As New PdfTextElement(0, 0, _
                        "Regular text using standard font Helvetica, size 20.", font1)
                result = page.Add(text1)

                ' create text element 
                Dim text2 As New PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, _
                        "Regular text using system font Verdana, size 15.", font2)
                result = page.Add(text2)

                ' create colored text element 
                Dim text3 As New PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, 200, _
                        "Red text over light blue background, using system font Verdana, size 15, limited at a width of 200 points.", font2)
                text3.ForeColor = System.Drawing.Color.Red
                text3.BackColor = System.Drawing.Color.LightBlue
                result = page.Add(text3)

                ' create colored text element, right aligned
                Dim text4 As New PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, 300, _
                        "Red text over light green background, right aligned, using system font Verdana, size 15, limited at a width of 300 points.", font2)
                text4.ForeColor = System.Drawing.Color.Red
                text4.BackColor = System.Drawing.Color.LightGreen
                text4.HorizontalAlign = PdfTextHorizontalAlign.Right
                result = page.Add(text4)

                ' create text element 
                Dim text5 As New PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 200, 300, _
                        "Regular text using system font Verdana, size 15, rotated 45 degrees.", font2)
                text5.Direction = 45
                ' rotate 45 degrees counter-clockwise
                result = page.Add(text5)

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

    End Class
End Namespace
