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?.