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

namespace SelectPdf.Samples
{
    public partial class convert_multiple_urls_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 url1 = TxtUrl1.Text;
            string url2 = TxtUrl2.Text;
            string url3 = TxtUrl3.Text;

            // create a new pdf document 
            PdfDocument doc = new PdfDocument();

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

            // add the first url
            PdfHtmlElement html1 = new PdfHtmlElement(url1);

            html1.RenderingEngine = renderingEngine;
            html1.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings["CefEnginePath"]);
            
            PdfRenderingResult result = page.Add(html1);

            // get the last page of the previous conversion
            PdfPage page2 = doc.Pages[result.PdfPageLastIndex];

            // add the second url
            PdfHtmlElement html2 = new PdfHtmlElement(result.PdfPageLastRectangle.Left, 
                result.PdfPageLastRectangle.Bottom, url2);

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

            result = page2.Add(html2);

            // get the last page of the previous conversion
            PdfPage page3 = doc.Pages[result.PdfPageLastIndex];

            // add the third url
            PdfHtmlElement html3 = new PdfHtmlElement(result.PdfPageLastRectangle.Left, 
                result.PdfPageLastRectangle.Bottom, url3);

            html3.RenderingEngine = renderingEngine;
            html3.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings["CefEnginePath"]);
            
            result = page3.Add(html3);

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

            // close pdf document
            doc.Close();

        }
    }
}