We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

WordDocument can not be parsed to json


using System;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace syncfusion_test.Controllers{
[Route("api/[controller]")]
public class ImportController : Controller{

[HttpPost]
public IActionResult Import(IFormFile file){
Stream stream = new MemoryStream();
int index = file.FileName.LastIndexOf('.');
var type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";
file.CopyTo(stream);
stream.Position = 0;

WordDocument document = new WordDocument(stream,GetFormatType(type));
var sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return Ok(sfdt);

}

static FormatType GetFormatType(string format){
if (string.IsNullOrEmpty(format))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (format.ToLower()) {
case ".dotx":
case ".docx":
case ".docm":
case ".dotm":
return FormatType.Docx;
case ".dot":
case ".doc":
return FormatType.Doc;
case ".rtf":
return FormatType.Rtf;
case ".txt":
return FormatType.Txt;
case ".xml":
return FormatType.WordML;
default:
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
}
}
}
}

Running this, show this error

Exception has occurred: CLR/Newtonsoft.Json.JsonSerializationException
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code: 'Error getting value from 'Font' on 'Syncfusion.DocIO.DLS.WCharacterFormat'.' Inner exceptions found, see $exception in variables window for more details. Innermost exception System.NullReferenceException : Object reference not set to an instance of an object.
at Syncfusion.DocIO.DLS.FontSettings.GetFont(String fontName, Single fontSize, FontStyle fontStyle)
at Syncfusion.DocIO.DLS.WCharacterFormat.get_Font()
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)

What is wrong?.


2 Replies

SM Suriya Murugan Syncfusion Team December 10, 2019 08:32 AM UTC



Thank you for contacting the Syncfusion support. 


## Import word document 
 
 
To convert the word document to Json, you have to use the nuget Syncfusion.EJ2.WordEditor.AspNet.Core. 
 
 
In provided sample code, you have referred the word document from Syncfusion DocIO. 
 
 
Please refer the below sample which contains code for Document importing. 
 
 
 
Please let me know if you have any questions. 
 

Regards, 

Suriya M. 



DF Dagmar Fernandez December 11, 2019 01:47 AM UTC

Thanks. I was resolved

Loader.
Live Chat Icon For mobile
Up arrow icon