Free Support Forum - aspose.com

Read word doc and load into RichTextbox with complete formatting

Hi


I need to read a word document and display doc content in richtextbox with complete formatting.
Also i need to save back the Rtf content back to Word doc after editing.
Right now i just able to read plain contents from doc using Aspose(using Paragraph.range.text etc).

Thanks for your help in advance .

This message was posted using Page2Forum from Feature Overview - Aspose.Words for .NET

Hi

Thanks for your request. In your case, you can save RTF to stream and then extract RTF string from this stream. Code will look like this:

private static string GetRtfString(string html)

{

string rtfString = "";

// Create a document and insert HTML into it.

DocumentBuilder builder = new DocumentBuilder();

builder.InsertHtml(html);

// Save document to stream as RTF and get RTF string.

using (MemoryStream rtfStream = new MemoryStream())

{

builder.Document.Save(rtfStream, SaveFormat.Rtf);

byte[] rtfBytes = rtfStream.ToArray();

rtfString = Encoding.UTF8.GetString(rtfBytes);

}

return rtfString;

}

Than after editing, you can load RTF back into the Document object and save it inany format supported by Aspose.Words. Code will look like this:

private static Document RtfStringToDocument(string rtf)

{

Document doc = null;

// Convert RTF string to byte array.

byte[] rtfBytes = Encoding.UTF8.GetBytes(rtf);

// Create stream.

using(MemoryStream rtfStream = new MemoryStream(rtfBytes))

{

// Open document from stream.

doc = new Document(rtfStream);

}

return doc;

}

Hope this helps.

Best regards,

Hi

Now its showing me the complete rtf string.
What i want is simple: Richtextbox content should look like exactly same as word doc.
i.e if Text is bold or italic or underline etc in the word doc same must be displayed in the richtextbox.

Thanks
Timothy Delixus

Hi Timothy,

Thanks for your request. What format is used by your ReachTextBox? Maybe your textbox uses HTML, but not RTF. In this case, you can use the same technique to convert a document into HTML string.

Best regards,