        using DevExpress.Export;
using DevExpress.Web;
using DevExpress.XtraCharts.Native;
using DevExpress.XtraPrinting;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OlympiaIntranet
{
    public partial class EsposizioneCertificati : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated == false)
            {
                Response.Redirect("~/SignIn.aspx");
            }
        }

        protected void btnCaricaDati_Click(object sender, EventArgs e)
        {
            Session["cod_rap"] = txtPortafoglio.Text;

            gvEsposizioneCertificati.DataBind();

            if (gvEsposizioneCertificati.VisibleRowCount == 0)
            {
                CommonClass.MessageBox(this, "Consultare la posizione in Guardian e ripetere la richiesta!");
                //  throw new MyException("Consultare la posizione in Guardian e ripetere la richiesta!");
            }
        }



        protected void btnEsportaExcel_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.ExportSelectedRowsOnly = false;
            ASPxGridViewExporter1.FileName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - Esposizione Certificati";
            XlsxExportOptionsEx op = new XlsxExportOptionsEx() { ExportType = ExportType.WYSIWYG };
            ASPxGridViewExporter1.WriteXlsxToResponse(op);
        }

        protected void gvEsposizioneCertificati_CustomErrorText(object sender, ASPxGridViewCustomErrorTextEventArgs e)
        {
            // https://supportcenter.devexpress.com/ticket/details/e59/how-to-show-an-error-message-when-a-record-cannot-be-deleted
            if (e.Exception is MyException) e.ErrorText = e.Exception.Message;
        }

        protected void btnEsportaPdf_Click(object sender, EventArgs e)
        {
            // Crea pdf di gvEsposizioneCertificati
            string gridFileName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - grid.pdf";
            using (System.IO.FileStream output = new System.IO.FileStream(Server.MapPath(@"~/App_Data/UploadTemp/" +gridFileName), FileMode.Create))
            {
                gvEsposizioneCertificati.ExportToPdf(output);
            }

            // Crea pdf del grafico
            string chartFileName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - chart.pdf";
            chart.DataBind();

            // Esporta grafico in pdf in landscape
            var ps = new PrintingSystem();
            ps.PageSettings.Landscape = true;
            var link1 = new PrintableComponentLink();            
            chart.DataBind();
            link1.Component = ((IChartContainer)chart).Chart;
            link1.Landscape = true;
            //link1.PaperKind = System.Drawing.Printing.PaperKind.A4Extra;
            //((Chart)link1.Component).OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Zoom;
            link1.PrintingSystem = ps;
            link1.CreateDocument();
            ps.Document.AutoFitToPagesWidth = 1; // Fit to page 
            ps.Document.ScaleFactor = 0.95F;
            using (FileStream stream = new FileStream(Server.MapPath(@"~/App_Data/UploadTemp/" + chartFileName), FileMode.Create))
            {
                link1.PrintingSystem.ExportToPdf(stream);
            }
            ps.Dispose();

            // Unisce i pdf della griglia e del grafico
            string finalPdfFinalName = DateTime.Now.ToString("yyyyMMdd_HHmm") + " - EsposizioneCertificati.pdf";
            PdfDocument one = new PdfDocument();
            one = PdfReader.Open(Server.MapPath(@"~/App_Data/UploadTemp/" + gridFileName), PdfDocumentOpenMode.Import); // pdf griglia
            PdfDocument two = new PdfDocument();
            two = PdfReader.Open(Server.MapPath(@"~/App_Data/UploadTemp/" + chartFileName), PdfDocumentOpenMode.Import); // pdf grafico
            PdfDocument three = new PdfDocument();
            using (PdfDocument outPdf = new PdfDocument())
            {
                CommonClass.CopyPages(one, outPdf);
                CommonClass.CopyPages(two, outPdf);
                outPdf.Save(Server.MapPath(@"~/App_Data/UploadTemp/" + finalPdfFinalName));
            }
            File.Delete(Server.MapPath(@"~/App_Data/UploadTemp/" + gridFileName));
            File.Delete(Server.MapPath(@"~/App_Data/UploadTemp/" + chartFileName));

            // Restituisce pdf finale da scaricare 
            byte[] bytePDF = System.IO.File.ReadAllBytes(Server.MapPath(@"~/App_Data/UploadTemp/" + finalPdfFinalName));

            // Restituisce pdf
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", $"attachment; filename={finalPdfFinalName}");
            Response.BinaryWrite(bytePDF);
            Response.Flush();
            Response.End();
        }
    }
}