using System;
using System.Web.Mvc;
using System.Configuration;
using SelectPdf;

namespace SelectPdf.Samples.Controllers
{
    public class PdfResizeController : Controller
    {
        // GET: /PdfResize/
        public ActionResult Index()
        {
            return View();
        }

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

            // settings
            string pdf_page_size = collection["DdlPageSize"];
            PdfPageSize pdfPageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                pdf_page_size, true);

            PdfCustomPageSize pageSize = PdfCustomPageSize.A4;
            switch (pdfPageSize)
            {
                case PdfPageSize.A1:
                    pageSize = PdfCustomPageSize.A1;
                    break;
                case PdfPageSize.A2:
                    pageSize = PdfCustomPageSize.A2;
                    break;
                case PdfPageSize.A3:
                    pageSize = PdfCustomPageSize.A3;
                    break;
                case PdfPageSize.A5:
                    pageSize = PdfCustomPageSize.A5;
                    break;
                case PdfPageSize.Letter:
                    pageSize = PdfCustomPageSize.Letter;
                    break;
                case PdfPageSize.HalfLetter:
                    pageSize = PdfCustomPageSize.HalfLetter;
                    break;
                case PdfPageSize.Ledger:
                    pageSize = PdfCustomPageSize.Ledger;
                    break;
                case PdfPageSize.Legal:
                    pageSize = PdfCustomPageSize.Legal;
                    break;
            }

            string pdf_orientation = collection["DdlPageOrientation"];
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);

            int topMargin = 0;
            try
            {
                topMargin = Int32.Parse(collection["TxtTopMargin"]);
            }
            catch { }

            int rightMargin = 0;
            try
            {
                rightMargin = Int32.Parse(collection["TxtRightMargin"]);
            }
            catch { }

            int bottomMargin = 0;
            try
            {
                bottomMargin = Int32.Parse(collection["TxtBottomMargin"]);
            }
            catch { }

            int leftMargin = 0;
            try
            {
                leftMargin = Int32.Parse(collection["TxtLeftMargin"]);
            }
            catch { }

            string horizontal_align = collection["DdlHorizontalAlign"];
            PdfHorizontalAlign pdfHorizontalAlign =
                (PdfHorizontalAlign)Enum.Parse(typeof(PdfHorizontalAlign),
                horizontal_align, true);

            string vertical_align = collection["DdlVerticalAlign"];
            PdfVerticalAlign pdfVerticalAlign =
                (PdfVerticalAlign)Enum.Parse(typeof(PdfVerticalAlign),
                vertical_align, true);

            // resize the content
            PdfResizeManager resizer = new PdfResizeManager();
            resizer.Load(filePdf);

            resizer.PageMargins = new PdfMargins(
                leftMargin, rightMargin, topMargin, bottomMargin);
            resizer.PageSize = pageSize;
            resizer.PageOrientation = pdfOrientation;
            resizer.AllowScale = collection["ChkAllowScaling"] == "on";
            resizer.KeepAspectRatio = collection["ChkKeepAspectRatio"] == "on";
            resizer.HorizontalAlign = pdfHorizontalAlign;
            resizer.VerticalAlign = pdfVerticalAlign;

            // save pdf document
            byte[] pdf = resizer.Save();

            // close pdf document
            resizer.Close();
            
            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}
