using SelectPdf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectPdf.Samples
{
    public partial class web_elements_location : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TxtUrl.Text = "https://en.wikipedia.org/wiki/PDF";
            }
        }

        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            RenderingEngine renderingEngine =
                (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine.SelectedValue, true);

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            converter.Options.RenderingEngine = renderingEngine;

            // set the css selectors for the web elements
            // whose positions will be retrieved
            converter.Options.WebElementsMappingOptions.CssSelectors =
                TxtElements.Text.Split(new char[] { ',' });

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl.Text);

            // get all web elements and mark/highlight them with a green rectangle
            foreach (WebElement element in 
                converter.Options.WebElementsMappingOptions.Result)
            {
                // each web element has several rendering rectangles 
                // (if it is displayed in more than 1 page)
                foreach (WebElementPdfRectangle webElementRectangle 
                    in element.PdfRectangles)
                {
                    // get the PDF page
                    PdfPage page = doc.Pages[webElementRectangle.PageIndex];
                    RectangleF rectangle = webElementRectangle.Rectangle;

                    // highlight the HTML element
                    PdfRectangleElement blueRectangle = new PdfRectangleElement(
                        rectangle.X, rectangle.Y,
                        rectangle.Width, rectangle.Height);
                    blueRectangle.ForeColor = Color.Blue;

                    page.Add(blueRectangle);
                }
            }

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
    }
}