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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Word Files serialize document object error

Thread ID:

Created:

Updated:

Platform:

Replies:

147885 Sep 26,2019 08:03 PM UTC Jan 15,2020 09:03 AM UTC ASP.NET Core - EJ 2 3
loading
Tags: DocumentEditor
James Brightley
Asked On September 26, 2019 08:03 PM UTC

I'm trying to convert HTML to the SFDT word standard to then load in the vue document editor but I'm getting an error when trying to serialize the docuemtn object

Code
public async TaskConvertHtmlToSfdt(string html)
{
byte[] byteArray = Encoding.ASCII.GetBytes(html);
using (var stream = new MemoryStream(byteArray))
{
WordDocument document = new WordDocument(stream, FormatType.Html);
using (var saveStream = new MemoryStream())
{
document.Save(saveStream, FormatType.Docx);
document.Close();
string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return sfdt;
}
}
}
Value of 'html' being passed to this function

My First Heading

My first paragraph.


Exception
Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Error getting value from 'Font' on 'Syncfusion.DocIO.DLS.WCharacterFormat'.
Source=Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Tprofile.BLL.Services.DocumentManipulationService.d__2.MoveNext() in \Services\DocumentManipulationService.cs:line 48
Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.


Suriya Murugan [Syncfusion]
Replied On September 27, 2019 12:56 PM UTC

Hi James, 
 
Thank you for contacting the Synfusion support. 
 
Using Syncfusion DocIO, you can convert the Html Document to Sfdt and load the sample DocumentEditor. 
  
Please check below sample code for reference. 
byte[] byteArray = Encoding.ASCII.GetBytes(html); 
using (var stream = new MemoryStream(byteArray)) 
{ 
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Html); 
using (var saveStream = new MemoryStream()) 
{ 
document.Save(saveStream, FormatType.Docx); 
document.Close(); 
Syncfusion.EJ2.DocumentEditor.WordDocument wdocument = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(saveStream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx); 
string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument); 
wdocument.Dispose(); 
return sfdt; 
} 
} 
  
Please check the below sample for reference which is in ASP.NET MVC. 
  
Please let me know if you have any questions. 
 
Regards, 
Suriya M. 


Daniel Cox
Replied On January 14, 2020 04:17 PM UTC

Hi James, 
 
Thank you for contacting the Synfusion support. 
 
Using Syncfusion DocIO, you can convert the Html Document to Sfdt and load the sample DocumentEditor. 
  
Please check below sample code for reference. 
byte[] byteArray = Encoding.ASCII.GetBytes(html); 
using (var stream = new MemoryStream(byteArray)) 
{ 
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Html); 
using (var saveStream = new MemoryStream()) 
{ 
document.Save(saveStream, FormatType.Docx); 
document.Close(); 
Syncfusion.EJ2.DocumentEditor.WordDocument wdocument = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(saveStream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx); 
string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument); 
wdocument.Dispose(); 
return sfdt; 
} 
} 
  
Please check the below sample for reference which is in ASP.NET MVC. 
  
Please let me know if you have any questions. 
 
Regards, 
Suriya M. 


What if we are using docx and not html? This fails with just a simple hello world text in a Word Document generated with DocIO

_document = new WordDocument();
_section = _document.AddSection();
_paragraph = _section.AddParagraph();
_paragraph.ParagraphFormat.HorizontalAlignment = styleOptions.Alignment;
_paragraph.ParagraphFormat.BeforeSpacing = styleOptions.TopSpace;
var textRange = _paragraph.AppendText(text);
textRange.CharacterFormat.Font = new Font(styleOptions.FontFamily, styleOptions.FontSize, styleOptions.FontStyle, GraphicsUnit.Pixel, new byte());
using (var stream = new MemoryStream())
{
_document.Save(stream, FormatType.Docx);
_document.Close(); var document = new WordDocument();
document.Open(stream, FormatType.Automatic); string sfdt = JsonConvert.SerializeObject(document); <----This line throws the exception with message "Error getting value from 'Font' on 'Syncfusion.DocIO.DLS.WCharacterFormat'."
                    response.Document = sfdt;
}

Harini Chellappa [Syncfusion]
Replied On January 15, 2020 09:03 AM UTC

Hi James, 

Syncfusion greetings! 

As mentioned in the previous update, once document is saved in stream using DOCIO, you have to pass that stream to EJ2.DocumentEditor.WordDocument. 
  
In your provided source code, you have used docio document object and tried to convert the docio document object to SFDT.  This throws the exception. 
  
Provided Code 
  
using (var stream = new MemoryStream())
                {
                    _document.Save(stream, FormatType.Docx);
                    _document.Close();                    var document = new WordDocument();
                    document.Open(stream, FormatType.Automatic);                    string sfdt = JsonConvert.SerializeObject(document);    
  
Modified Code 
  
Syncfusion.EJ2.DocumentEditor.WordDocument wdocument = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(saveStream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx);  
string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(wdocument);  
wdocument.Dispose();  
  
  
Please check with modified code and let us know if you need any further assistance. 

Regards, 
Harini C 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon