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

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

                // define a rendering result object
                PdfRenderingResult result;

                // simple horizontal line element
                PdfLineElement line1 = new PdfLineElement(0, 0, 400, 0);
                line1.LineStyle.LineWidth = 3;
                result = page.Add(line1);

                // horizontal dotted red line element
                PdfLineElement line2 = new PdfLineElement(0, 30, 400, 30);
                line2.ForeColor = System.Drawing.Color.Red;
                line2.LineStyle.LineWidth = 2;
                line2.LineStyle.LineDashStyle = PdfLineDashStyle.Dot;
                result = page.Add(line2);

                // horizontal dashed blue line element with complex styles
                PdfLineElement line3 = new PdfLineElement(0, 60, 400, 60);
                line3.ForeColor = System.Drawing.Color.Blue;
                line3.LineStyle.LineWidth = 2;
                line3.LineStyle.LineDashStyle = PdfLineDashStyle.Dash;
                line3.LineStyle.LineCapStyle = PdfLineCapStyle.RoundCap;
                line3.LineStyle.LineJoinStyle = PdfLineJoinStyle.RoundJoin;
                result = page.Add(line3);

                // simple rectangle
                PdfRectangleElement rect1 = new PdfRectangleElement(0, 90, 400, 100);
                result = page.Add(rect1);

                // simple circle
                PdfCircleElement circ1 = new PdfCircleElement(50, 250, 50);
                circ1.ForeColor = System.Drawing.Color.Green;
                result = page.Add(circ1);

                // ellipse filled with gradient background color
                PdfEllipseElement elli1 = new PdfEllipseElement(300, 250, 100, 50);
                elli1.ForeColor = System.Drawing.Color.Brown;
                elli1.Gradient = new PdfGradient(PdfGradientDirection.ForwardDiagonal,
                    System.Drawing.Color.LightBlue, System.Drawing.Color.DarkBlue);
                elli1.LineStyle.LineWidth = 2;
                result = page.Add(elli1);

                // bezier curve
                PdfBezierCurveElement bez1 = new PdfBezierCurveElement(
                    50, 350, 100, 400, 200, 350, 250, 370);
                result = page.Add(bez1);

                // 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 GraphicElements()
        {
            InitializeComponent();
            try
            {
                // display highlighted sample code
                string code = Helper.ReadSourceCode("GraphicElements.cs");
                WBSampleCode.DocumentText = new CodeColorizer().Colorize(code, Languages.CSharp);
            }
            catch
            {
                WBSampleCode.DocumentText = "Sample code not available";
            }
        }

    }
}
