Description:
Some scenarios with ASPxRichEdit require ability to obtain its actual text content on the server side during a callback or postback processing. By the actual content, the content of an opened document with user modifications is meant.
Answer:
The task of retrieving ASPxRichEdit main content can be resolved by using the SaveCopy method. For example:
C#MemoryStream ms = new MemoryStream();
ASPxRichEdit1.SaveCopy(ms, DocumentFormat.PlainText);
ms.Position = 0;
string plainText = new StreamReader(ms).ReadToEnd();
Visual BasicDim ms As New MemoryStream()
ASPxRichEdit1.SaveCopy(ms, DocumentFormat.PlainText)
ms.Position = 0
Dim plainText As String = New StreamReader(ms).ReadToEnd()
However, this approach returns only the main content text without the text displayed in the document's header and footer. To access the text, displayed in these sections, it's necessary to get the document's stream by using the SaveCopy method and open it via RichEditDocumentServer:
C#MemoryStream stream = new MemoryStream();
ASPxRichEdit1.SaveCopy(stream, DocumentFormat.OpenXml);
RichEditDocumentServer server = new RichEditDocumentServer();
stream.Position = 0;
server.LoadDocument(stream, DocumentFormat.OpenXml);
Visual BasicDim stream As New MemoryStream()
ASPxRichEdit1.SaveCopy(stream, DocumentFormat.OpenXml)
Dim server As New RichEditDocumentServer()
stream.Position = 0
server.LoadDocument(stream, DocumentFormat.OpenXml)
Then, obtain the header and footer text from the RichEditDocumentServer's Document by using the following methods:
Section.BeginUpdateHeader Method
Section.BeginUpdateFooter Method
So, the code to obtain a document's header's modified content is:
C#SubDocument docHead = server.Document.Sections[0].BeginUpdateHeader();
string textHead = docHead.GetText(docHead.Range);
server.Document.Sections[0].EndUpdateHeader(docHead);
Visual BasicDim docHead As SubDocument = server.Document.Sections(0).BeginUpdateHeader()
Dim textHead As String = docHead.GetText(docHead.Range)
server.Document.Sections(0).EndUpdateHeader(docHead)
To obtain the footer, you can use the following code:
C#SubDocument docFoot = server.Document.Sections[0].BeginUpdateFooter();
string textFoot = docFoot.GetText(docFoot.Range);
server.Document.Sections[0].EndUpdateFooter(docFoot);
Visual BasicDim docFoot As SubDocument = server.Document.Sections(0).BeginUpdateFooter()
Dim textFoot As String = docFoot.GetText(docFoot.Range)
server.Document.Sections(0).EndUpdateFooter(docFoot)