using System;
using System.IO;
using System.Drawing.Imaging;
using System.Web.Mvc;
using System.Configuration;
namespace SelectPdf.Samples.Controllers
{
    public class PdfToImageConverterController : Controller
    {
        // GET: PdfToImageConverter
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitAction(FormCollection collection)
        {
            // the test file
            string filePdf = Server.MapPath("~/files/selectpdf.pdf");

            // settings
            string imgFormat = collection["DdlImageFormat"];

            int startPage = 1;
            try
            {
                startPage = Convert.ToInt32(collection["TxtStartPage"]);
            }
            catch { }

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(collection["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();
            }
        }
    }
}