Search results

Using ASP.NET MVC in Vue DocumentEditor component

25 Oct 2021 / 3 minutes to read

Document Editor depends on server side interaction for below listed operations can be written in ASP.NET MVC using Syncfusion.EJ2.WordEditor.AspNet.Mvc5 or Syncfusion.EJ2.WordEditor.AspNet.Mvc4.

  • Import Word Document
  • Paste with formatting
  • Restrict Editing
  • Spell Check
  • Save as file formats other than SFDT and DOCX

This section explains how to create the service for Document Editor in ASP.NET MVC.

Importing Word Document

Word documents can be imported to Document Editor using the below code snippet.

Copied to clipboard
[HttpPost]
[EnableCors("*", "*", "*")]
[Route("Import")]
public HttpResponseMessage Import()
{
    if (HttpContext.Current.Request.Files.Count == 0)
        return null;

    HttpPostedFile file = HttpContext.Current.Request.Files[0];
    int index = file.FileName.LastIndexOf('.');
    string type = index > -1 && index < file.FileName.Length - 1 ?
        file.FileName.Substring(index) : ".docx";
    Stream stream = file.InputStream;
    stream.Position = 0;
    //Convert the word document in to sfdt.
    EJ2WordDocument document = EJ2WordDocument.Load(stream, GetFormatType(type.ToLower()));
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
    document.Dispose();
    return new HttpResponseMessage() { Content = new StringContent(json) };
}

Paste with formatting

Paste with formatting action is defined in the below code snippet.

Copied to clipboard
[HttpPost]
[EnableCors("*", "*", "*")]
[Route("SystemClipboard")]
public HttpResponseMessage SystemClipboard([FromBody]CustomParameter param)
{
    if (param.content != null && param.content != "")
    {
        try
        {
            //Convert the pasted content into sfdt.
            Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.LoadString(param.content, GetFormatType(param.type.ToLower()));
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
            document.Dispose();
            return new HttpResponseMessage() { Content = new StringContent(json) };
        }
        catch (Exception)
        {
            return new HttpResponseMessage() { Content = new StringContent("") };
        }

    }
    return new HttpResponseMessage() { Content = new StringContent("") };
}

public class CustomParameter
{
    public string content { get; set; }
    public string type { get; set; }
}

Restrict editing

Restrict editing action is defined in the below code snippet.

Copied to clipboard
[HttpPost]
[EnableCors("*", "*", "*")]
[Route("RestrictEditing")]
public string[] RestrictEditing([FromBody]CustomRestrictParameter param)
{
    if (param.passwordBase64 == "" && param.passwordBase64 == null)
        return null;
    //Compute hash value for the specfied password.
    return Syncfusion.EJ2.DocumentEditor.WordDocument.ComputeHash(param.passwordBase64, param.saltBase64, param.spinCount);
}

public class CustomRestrictParameter
{
    public string passwordBase64 { get; set; }
    public string saltBase64 { get; set; }
    public int spinCount { get; set; }
}

Spell Check

Spell check action is defined in the below code snippet.

Copied to clipboard
[HttpPost]
[EnableCors("*", "*", "*")]
[Route("SpellCheck")]
public HttpResponseMessage SpellCheck([FromBody] SpellCheckJsonData spellChecker)
{
    try
    {
        SpellChecker spellCheck = new SpellChecker(spellDictionary);
        //Check spelling and get suggestions for the specified text.
        spellCheck.GetSuggestions(spellChecker.LanguageID, spellChecker.TexttoCheck, spellChecker.CheckSpelling, spellChecker.CheckSuggestion, spellChecker.AddWord);
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(spellCheck);
        return new HttpResponseMessage() { Content = new StringContent(json) };
    }
    catch
    {
        return new HttpResponseMessage() { Content = new StringContent("{\"SpellCollection\":[],\"HasSpellingError\":false,\"Suggestions\":null}") };
    }
}

[HttpPost]
[EnableCors("*", "*", "*")]
[Route("SpellCheckByPage")]
public HttpResponseMessage SpellCheckByPage([FromBody] SpellCheckJsonData spellChecker)
{
    try
    {
        SpellChecker spellCheck = new SpellChecker(spellDictionary);
        //Check spelling for the specified text
        spellCheck.CheckSpelling(spellChecker.LanguageID, spellChecker.TexttoCheck);
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(spellCheck);
        return new HttpResponseMessage() { Content = new StringContent(json) };
    }
    catch
    {
        return new HttpResponseMessage() { Content = new StringContent("{\"SpellCollection\":[],\"HasSpellingError\":false,\"Suggestions\":null}") };
    }
}

public class SpellCheckJsonData
{
    public int LanguageID { get; set; }
    public string TexttoCheck { get; set; }
    public bool CheckSpelling { get; set; }
    public bool CheckSuggestion { get; set; }
    public bool AddWord { get; set; }

}

Note: Please refer the ASP.NET MVC Web API sample.