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

                // create a new pdf font (component standard font)
                PdfFont font1 = doc.AddFont(PdfStandardFont.Helvetica);
                font1.Size = 20;

                // create a new pdf font (system font)
                PdfFont font2 = doc.AddFont(new System.Drawing.Font("Verdana", 15));

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

                // define a rendering result object
                PdfRenderingResult result;

                // create text element 
                PdfTextElement text1 = new PdfTextElement(0, 0,
                    "Regular text using standard font Helvetica, size 20.", font1);
                result = page.Add(text1);

                // create text element 
                PdfTextElement text2 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20,
                    "Regular text using system font Verdana, size 15.", font2);
                result = page.Add(text2);

                // create colored text element 
                PdfTextElement text3 = 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
                PdfTextElement text4 = 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 
                PdfTextElement text5 = 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 (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 PdfTextElementForm()
        {
            InitializeComponent();
            try
            {
                // display highlighted sample code
                string code = Helper.ReadSourceCode("PdfTextElementForm.cs");
                WBSampleCode.DocumentText = new CodeColorizer().Colorize(code, Languages.CSharp);
            }
            catch
            {
                WBSampleCode.DocumentText = "Sample code not available";
            }
        }

    }
}
