0

In my MVC application I want to display the extracted text in CKEDITOR. but the text is getting diplayed in textarea and not in editor My controller code is:

 public ActionResult ExtractText(string fn)
    {

        string extFile = Server.MapPath(_fileUploadPath + fn);
        string filePath = Path.Combine(HttpContext.Server.MapPath(_fileUploadPath), Path.GetFileName(fn));
        if (filePath != null)
        {

            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            f.OpenPdf(System.IO.File.ReadAllBytes(filePath));

            string text = f.ToText();
            string sValue = "<textarea id = \"temp_edit\" name=\"content\" cols=\"73\" rows=\"15\">" + text + "</textarea> <script type=\"text/javascript\">CKEDITOR.replace('temp_edit');</script><input class=\"margin-top-05 green_button\" type=\"button\" value=\"Save\" onclick=\"save_file()\" /><a class=\"close\" onclick=\"parent.$.fancybox.close();\"><img class=\"close_image\" title=\"close\" src=\"../images/closelabel.gif\" style=\"width: auto; height: auto; position: absolute; bottom: 0px; right: 0px;\"></a>";
           return Content(sValue);
        }
        else
        {
            TempData["UploadValidationMessage_Failure"] = "File does not exist";
            return View();

        }
    }

1 Answers1

4

The textarea styles, javascript events can be done on your view. Pass the text to the view and show it on the textarea. All your events and styles can be written on the view. The ckeditor can be loaded to the textarea on ready function. Please go through the following.

CK Editor for .Net MVC

For a better way to implement CKEditor in your project, please go through the aswer in the following link

CKEditor MVC 3 implementaion Help needed

Edit..

<%= Html.ActionLink("Extract Text", "ExtractText", new { fn = file })%>

takes you to your function.

Lets say, you have a model NewContent

public class NewContent
{
 public string Text
    {
        get;
        set;
    }
}

Return the NewContent object with you text from the controller.

 public ActionResult ExtractText(string fn)
    {
        string extFile = Server.MapPath(_fileUploadPath + fn);
        string filePath = Path.Combine(HttpContext.Server.MapPath(_fileUploadPath), Path.GetFileName(fn));
        if (filePath != null)
        {
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            f.OpenPdf(System.IO.File.ReadAllBytes(filePath));
            string text = f.ToText();
            NewContent content = new NewContent();
            content.Text = text;
            return View(content);
        }
        else
        {
            TempData["UploadValidationMessage_Failure"] = "File does not exist";
            return View();

        }
    }

In your view, add the following

 <script src="ckeditor/ckeditor.js"></script>
 <script src="ckeditor/adapters/jquery.js"></script>

<%=Html.TextAreaFor(c => c.Text) %>

 <script type="text/javascript">
    $(function () {
        $('#Text').ckeditor();
    });
</script>

You will get your text from controller in the view in a ck editor. Make sure you have all necessary ckeditor scripts and its location provided correctly

Community
  • 1
  • 1
Rifaj
  • 1,024
  • 12
  • 21
  • I want to display the text in textarea.. have included javascript in controller itself but its not working .. –  Mar 20 '13 at 07:56
  • Always use javascript in the views. Pass the text to your controller and show it on the textarea. In ready function of your view, provide script to change your text area with the "text" to change to ckeditor – Rifaj Mar 20 '13 at 08:59
  • in view i have <%= Html.ActionLink("Extract Text", "ExtractText", new { fn = file })%> which calls ExtractText method from controller –  Mar 20 '13 at 09:15
  • Please explain how you need the content in the view. As per my understanding, you will click a link and a text needed to be shown in the ckeditor in a different view. – Rifaj Mar 20 '13 at 10:51
  • yes correct.. Whenever user clicks on the Extract Text link the data from the file should get extracted and should be shown in the editor –  Mar 20 '13 at 11:26
  • @user2031327 Ok..There are two ways to implement this. One, onclick you can get the text using ajax and show it in your ckeditor already loaded. Second one, onclick you can redirect to ExtractText(string fn) itself and on return, you can bring the text in a model which can be binded to your view. I will edit my Answer accordingly. – Rifaj Mar 20 '13 at 12:27
  • now am getting error for <%=Html.TextAreaFor(c => c.Text) %> and the page is not getting loaded also –  Mar 21 '13 at 05:07
  • error is Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) –  Mar 21 '13 at 05:08
  • That error wont come from NewContent content = new NewContent(); content.Text = text; return View(content); Check your functions to extract text from PDF – Rifaj Mar 21 '13 at 06:46