using System.IO;
using System.Drawing.Imaging;
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace SelectPdf.Samples.Pages
{
    public class PdfToImageConverterModel : PageModel
    {
        public void OnGet()
        {

        }

        [BindProperty]
        public string DdlImageFormat { get; set; }

        public List<SelectListItem> ImageFormats { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "PNG", Text = "PNG" },
            new SelectListItem { Value = "JPEG", Text = "JPEG" },
            new SelectListItem { Value = "BMP", Text = "BMP" },
            new SelectListItem { Value = "TIFF", Text = "TIFF" },
        };

        [BindProperty]
        public string TxtStartPage { get; set; }

        [BindProperty]
        public string TxtEndPage { get; set; }


        public IActionResult OnPost()
        {
            // the test file
            string filePdf = Startup.ServerPath + @"\wwwroot\files\selectpdf.pdf";

            // settings
            string imgFormat = DdlImageFormat;

            int startPage = 1;
            try
            {
                startPage = Convert.ToInt32(TxtStartPage);
            }
            catch { }

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(TxtEndPage);
            }
            catch { }

            // instantiate a pdf rasterizer (pdf to image converter) object
            PdfRasterizer rasterizer = new PdfRasterizer();

            // load PDF file
            rasterizer.Load(filePdf);

            // set the properties
            rasterizer.StartPageNumber = startPage;
            rasterizer.EndPageNumber = endPage;

            // other properties that can be set
            rasterizer.Resolution = 150;
            rasterizer.ColorSpace = PdfRasterizerColorSpace.RGB;

            // convert pages to images
            byte[] imgBytes;
            string httpHeader;
            string fileName;
            if (imgFormat == "TIFF")
            {
                // TIFF image format
                imgBytes = rasterizer.ConvertToTiff();
                httpHeader = "image/tiff";
                fileName = "Image.tiff";
            }
            else
            {
                // the other image formats (PNG, JPG, BMP)
                System.Drawing.Image[] images = rasterizer.ConvertToImages();

                System.Drawing.Imaging.ImageFormat format;
                if (imgFormat == "PNG")
                {
                    format = System.Drawing.Imaging.ImageFormat.Png;
                    httpHeader = "image/png";
                    fileName = "Image.png";
                }
                else if (imgFormat == "JPG")
                {
                    format = System.Drawing.Imaging.ImageFormat.Jpeg;
                    httpHeader = "image/jpeg";
                    fileName = "Image.jpg";
                }
                else
                {
                    format = System.Drawing.Imaging.ImageFormat.Bmp;
                    httpHeader = "image/bmp";
                    fileName = "Image.bmp";
                }

                imgBytes = GetImageBytes(images[0], format);
            }

            // return resulted image
            FileResult fileResult = new FileContentResult(imgBytes, httpHeader);
            fileResult.FileDownloadName = fileName;
            return fileResult;

        }

        private byte[] GetImageBytes(System.Drawing.Image image, ImageFormat format)
        {
            using (var ms = new MemoryStream())
            {
                image.Save(ms, format);
                return ms.ToArray();
            }
        }

    }
}