get content of

how to retrieve the content of the documenteditorcontainer and assign it to a local variable of type string ?
The save method is of type void and therefore does not allow me to retrieve the content of the editor.

sample :
1) Set doc into editor :
     this.editorDefault.documentEditor.open(DocumentFromDB); => it works fine

2) Get doc from editor :
     ???
     this.myprop = this.editorDefault.documentEditor.get(); => something like this ??
    

3 Replies 1 reply marked as answer

KB Kurthis Banu Abdul Majeeth Syncfusion Team April 28, 2021 08:00 AM UTC

Hi michel, 

We are cross checked your requirement. Kindly check the below sample for your requirement. 


Please find the code change in “app.component.ts”  file  line number 33 . 


In this sample, you can retrieve the content of the documenteditorcontainer and assign it to a local variable. 

Kindly use the below code snippet to achieve to requirement. 

Code snippet: 

 let json: string = this.container.documentEditor.serialize(); 
 this.container.documentEditor.open(json); 
 


Please let us know whether this will satisfy your requirement. 

Regards, 
Kurthis Banu A. 


Marked as answer

ML Michel Lochten April 29, 2021 06:11 AM UTC

Thanks for the quick response, everything is ok, but.....

Nevertheless, there is still a grey area in the transformation of the content from the editor to my database.
Here is the code used to read the content from the DB and then save it.
In the DB the field that will contain the result is of type nvarchar(max).

READ

Angular :
 this.editorDefault.documentEditor.open(result.Editordocument);

C#:
     public static string ConvertStringToSyncFusionDoc(string text)
        {
            WordDocument document = WordDocument.LoadString(text, FormatType.Rtf);
            return JsonConvert.SerializeObject(document);
        }

WRITE

Angular :
    let editordocumentstring = this.editorDefault.documentEditor.serialize(); 

C#:

     public static string ConvertSyncFusionDocToString(string text)
        {
            Stream stream = WordDocument.Save(text, FormatType.Rtf);
            StreamReader readerDoc = new(stream);
            return readerDoc.ReadToEnd();
        }


this code works perfectly, if the FormatType is RTF !
This was not the objective, other types of format never restore the document as it was saved ! first finding

Second one, I don't want to use an nvarchar (double storage UTF16 !) field in my table, but a varbinary.
so I added after the conversion of the content of the editor, a conversion of the string to a bytearray.

C#
public static string ConvertBinaryToString(byte[] byteArray) => Encoding.Unicode.GetString(byteArray);
public static byte[] ConvertStringToBinary(string text) => Encoding.Unicode.GetBytes(text);

but now it's a disaster, I don't get anything when I read it. Why ?
for info, I use this method with CKEditor and it works correctly.









SM Suriya Murugan Syncfusion Team May 3, 2021 11:18 AM UTC

Hi Michel, 

Read: 

Using Load API, in backend which will accept all the Documenteditor supported format. 

Convert the input varbinary to stream and pass that stream to Load API. 
Code snippet for reference: 

// Pass your stream here 
Stream stream = new MemoryStream(byteArray(provide your byte array here)); 
 
WordDocument document = WordDocument.Load(stream, GetFormatType(type)); 
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); 
            // Releases unmanaged and optionally managed resources. 
            document.Dispose(); 
            stream.Close(); 
// will get sfdt to load in Documenteditor 
return json; 
 
internal static Syncfusion.EJ2.DocumentEditor.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 Syncfusion.EJ2.DocumentEditor.FormatType.Docx; 
                case ".dot": 
                case ".doc": 
                    return Syncfusion.EJ2.DocumentEditor.FormatType.Doc; 
                case ".rtf": 
                    return Syncfusion.EJ2.DocumentEditor.FormatType.Rtf; 
                case ".txt": 
                    return Syncfusion.EJ2.DocumentEditor.FormatType.Txt; 
                case ".xml": 
                    return Syncfusion.EJ2.DocumentEditor.FormatType.WordML; 
 
                case ".html": 
                    return Syncfusion.EJ2.DocumentEditor.FormatType.Html; 
                default: 
                    throw new NotSupportedException("EJ2 DocumentEditor does not support this file format."); 
           
       


Write: 
 
Code snippet for reference: 
Stream document = Syncfusion.EJ2.DocumentEditor.WordDocument.Save(data.content, //Provide your required format type here); 
 
// it will return byte array 
        return ms.ToArray(); 
 
From this stream you can convert to varbinary for your requirement 


Can you please check it and let us know if you have any questions? 

Regards, 
Suriya M. 


Loader.
Up arrow icon