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

Word Files serialize document object error

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.


6 Replies

SM Suriya Murugan Syncfusion Team 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. 



DC Daniel Cox replied to Suriya Murugan 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;
}


HC Harini Chellappa Syncfusion Team 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 



HA Halber Albarracin October 12, 2022 02:58 AM UTC

Hi, i am getting the same issue and I am trying to use the example but I don't find the class 

Syncfusion.EJ2.DocumentEditor.WordDocument , only find 

Syncfusion.DocIO.DLS.WordDocument 


can you help me with some idea?

I'm  working with .net framework 4.0 and asp.net web forms

thanks


Attachment: screenshots_12e0af65.7z


DS Dhanush Sekar Syncfusion Team October 14, 2022 12:19 PM UTC

Hi Halber,


##i am getting the same issue and I am trying to use the example but I don't find the class 

Syncfusion.EJ2.DocumentEditor.WordDocument


We are suspecting that you have not downloaded the document editor NuGet package in your project. Please refer the below documentation for reference 

https://ej2.syncfusion.com/aspnetcore/documentation/document-editor/getting-started-core#install-aspnet-core-package-in-the-application

Please let us know if you need any further assistance


Regards,

Dhanush Sekar

 



SM Suriya Murugan Syncfusion Team October 17, 2022 04:05 AM UTC

Hi Halber,


Please install the below nuget to get out of this issue "I don't find the class Syncfusion.EJ2.DocumentEditor.WordDocument ":


https://www.nuget.org/packages/Syncfusion.Ej2.Wordeditor.ASPNet.Core/


Documentation:

https://ej2.syncfusion.com/aspnetcore/documentation/document-editor/import

Note: If this post is helpful, please mark it as an answer so that other members can locate it more quickly.


Please let us know if you need any further assistance.


Regards,

Suriya M.










Loader.
Live Chat Icon For mobile
Up arrow icon