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

namespace SelectPdf.Samples
{
    public partial class convert_url_to_pdf : System.Web.UI.Page
    {
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            RenderingEngine renderingEngine =
                (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine.SelectedValue, true);

            // read parameters from the webpage
            string url = TxtUrl.Text;

            string pdf_page_size = DdlPageSize.SelectedValue;
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), 
                pdf_page_size, true);

            string pdf_orientation = DdlPageOrientation.SelectedValue;
            PdfPageOrientation pdfOrientation = 
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), 
                pdf_orientation, true);

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32(TxtWidth.Text);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32(TxtHeight.Text);
            }
            catch { }

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

            converter.Options.RenderingEngine = renderingEngine;
            converter.Options.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings["CefEnginePath"]);

            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;

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

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

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