How to insert / save RTF string - ASPxRichEdit

1

In my application, I search the database for a string that contains RTF and needs to load it into ASPxRichEdit. And, when necessary, save the contents of the ASPxRichEdit into an RTF string to store in the database. How can I do this in C #?

I managed indirectly by creating files to open / save, but it is unfeasible because of performance. That is, the form below is impractical.

file.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    string rtf = BuscaTexto();
    string open = @"Projects/PCMSO/PCMSO/App_Data/WorkDirectory/open" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".rtf";
    StreamWriter writer = new StreamWriter(open);
    writer.WriteLine(rtf);
    writer.Close();
    ASPxRichEdit1.Open(open);

    Delete(open);
}

public string Save()
{
    string salvo = @"Projects/PCMSO/PCMSO/App_Data/WorkDirectory/save" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
    ASPxRichEdit1.SaveCopy(salvo);
    string rtf_string = System.IO.File.ReadAllText(salvo);

    Delete(salvo);

    return rtf_string;
}

file.aspx

<td>                                                                            
   <form runat="server">
      <dx:ASPxRichEdit ID="ASPxRichEdit1" style="width: 100%; height: 400px" runat="server" WorkDirectory="~\App_Data\WorkDirectory"></dx:ASPxRichEdit>
   </form>
</td>
    
asked by anonymous 26.01.2018 / 14:35

1 answer

1

I was able to insert the RTF string into the document, as follows:

file.aspx.cs

protected void ASPxRichEdit1_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
{
   string rtf = BuscaTexto();

   MemoryStream memoryStream = new MemoryStream();
   ASPxRichEdit1.SaveCopy(memoryStream, DocumentFormat.Rtf);
   memoryStream.Position = 0;

   var server = new RichEditDocumentServer();
   server.LoadDocument(memoryStream, DocumentFormat.Rtf);
   var pos = server.Document.CreatePosition(Convert.ToInt32(e.Parameter));
   server.Document.InsertRtfText(pos, rtf);

   memoryStream = new MemoryStream();
   server.SaveDocument(memoryStream, DocumentFormat.Rtf);
   ASPxRichEdit1.Open(Guid.NewGuid().ToString(), DocumentFormat.Rtf, () =>
   {
       return memoryStream.ToArray();
   });
}

file.aspx

<script>
    var startPosition = -1;
    function OnClick(s, e) {
        startPosition = rich.selection.intervals[0].start;
        rich.PerformCallback(startPosition);
    }
</script>

<td>
   <form runat="server">
        <dx:ASPxRichEdit ID="ASPxRichEdit1" ClientInstanceName="rich" style="width: 100%; height: 600px" runat="server" WorkDirectory="~\App_Data\WorkDirectory" OnCallback="ASPxRichEdit1_Callback"></dx:ASPxRichEdit>
   </form>
</td>

I was able to save RTF content into a string, as follows:

file.aspx

string t1 = Encoding.UTF8.GetString(ASPxRichEdit1.SaveCopy(DocumentFormat.Rtf));
    
29.01.2018 / 13:23