using System;
using System.Web.Mvc;

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

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

            // settings
            string text_layout = collection["DdlTextLayout"];
            TextLayout textLayout = (TextLayout)Enum.Parse(typeof(TextLayout),
                text_layout, true);

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

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(collection["TxtEndPage"]);
            }
            catch { }

            // instantiate a pdf to text converter object
            PdfToText pdfToText = new PdfToText();

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

            // set the properties
            pdfToText.Layout = textLayout;
            pdfToText.StartPageNumber = startPage;
            pdfToText.EndPageNumber = endPage;

            // extract the text
            string text = pdfToText.GetText();

            // convert text to UTF-8 bytes
            byte[] utf8 = System.Text.Encoding.UTF8.GetBytes(text);

            // return resulted text file
            FileResult fileResult = new FileContentResult(utf8, 
                "text/plain; charset=UTF-8");
            fileResult.FileDownloadName = "output.txt";
            return fileResult;
        }
    }
}