Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
//CONTROLLER
namespace hOP.Controllers
{
    public class FileUploadController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Parcourir()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Parcourir(ExtractPDFToText fileUploadModel)
        {string TEXT;
            ExtractPDFToText fft = fileUploadModel;
            if (fileUploadModel.File.ContentLength > 0)
            {
                string strText = string.Empty;
                var fileName = Path.GetFileName(fileUploadModel.File.FileName);
                if (!String.IsNullOrEmpty(fileName))
                {
                    //fft.Location = Path.GetFileNameWithoutExtension(fileName);
                    try
                    {
                        using (PdfReader reader = new PdfReader(fft.File.InputStream))
                        {
                            for (int page = 1; page <= reader.NumberOfPages; page++)
                            {
                                ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                                string s = PdfTextExtractor.GetTextFromPage(reader, page, its);
                                s = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(s)));
                                strText = strText + s;
                                fft.pdftext = strText;

                            }
                            reader.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.GetBaseException();
                    }
                    try
                    {
                        using (PdfReader reader1 = new PdfReader(fft.File.InputStream))
                        {
                            using (StreamWriter writer1 = new StreamWriter("direction.txt", false, Encoding.UTF8))
                            {
                            for (int page = 1; page <= reader1.NumberOfPages; page++)
                            {
                                    ITextExtractionStrategy its1 = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
                                    ITextExtractionStrategy its = new CSVTextExtractionStrategy();
                                    string PageCSVText = PdfTextExtractor.GetTextFromPage(reader1, page, its);
                                    System.Diagnostics.Debug.WriteLine(PageCSVText);
                                    writer1.WriteLine(PageCSVText);
                                }
                                reader1.Close();
                                writer1.Flush();
                                writer1.Close();

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ViewData["Message"] = " Exception!";
                    }
                }
            }
            return View(); 
        }
    }
}
//MODEL
public class ExtractPDFToText
    {
        [Display(Name = "Location")]
        public string Location { get; set; }
        [Required]
        [Display(Name = "File")]
        public HttpPostedFileBase File { get; set; }
        [Column(TypeName = "ntext")]
        [MaxLength]
        [Display(Name = "pdftext")]
        public string pdftext { get; set; }
    }
//VIEW
@model hOP.Models.ExtractPDFToText
@{
    ViewBag.Title = "Parcourir";
}

HTML
<h2>Parcourir</h2>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>FileUplaodModel</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Location)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Location)
            @Html.ValidationMessageFor(model => model.Location)
        </div>
        <div class="editor-field">
            @Html.LabelFor(model => model.File)
            @Html.TextBoxFor(model => model.File, new { type = "file" })<br />
        </div>
        <div class="editor-field">
          @Html.TextArea("Message", new { rows=10, columns=40 })
        </div>        
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
Posted
Updated 23-Apr-14 0:40am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900