Rtf to word ml

Hi,
Could you please explain how to convert a richtextbox text [rtf] to word ml using aspose.
Thanks in advance,
Ali

Hi
Thanks for your inquiry. You can use the following code:

// Open rtf document
Document doc = new Document("in.rtf");
// Save document in WML format
doc.Save("out.xml", SaveFormat.WordML);

Best regards.

Hi,
Thank you for your quick reply. I am really sorry that I actually didn’t explain what I was looking for. I actually want to convert rtf string in richtexbox control [ which may include bullets and all those rich text] without saving it to a rtf file to word doc. ie, whatever i see in a richtextbox should be seen in word doc. Please advice.
eg: the string in richtextbox

  • Hello World

should be the same in the Doc we save using Aspose.
Thank you very much for your support.
Warm Regards,
Ali

Hi
Thanks for your inquiry. You can try using the following code:

// Here you should get rtf string from your RichTextBox
string rtfString = File.ReadAllText(@"Test205\in.rtf");
// Convert string to byte array
byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
// Sreate stream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
// Create Aspose.Words.Document from stream
Document doc = new Document(rtfStream);
// Save document
doc.Save(@"Test205\out.doc");

Best regards.

That really works Alexey, Thank you very much.

Hi Alexy,
I tried your code and it works fine, but I tried to save the wordML to MemoryStream instead doc file since i need to get the string without saving it to external doc file and tried to read it, but output is wrong. I can simply explain with the help of below code.

string rtfString = richTextBox1.Rtf;
// Convert string to byte array
byte[] rtfBytes = System.Text.Encoding.UTF8.GetBytes(rtfString);
// Sreate stream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
StreamReader reader1 = new StreamReader(rtfStream);
// we get the string rtfString here to str1
string str1 = reader1.ReadToEnd();
// Create Aspose.Words.Document from stream
Document doc = new Document(rtfStream);
MemoryStream saveStream = new MemoryStream();
doc.Save(saveStream, SaveFormat.WordML);
StreamReader reader2 = new StreamReader(saveStream);
// here rtfString != str2
string str2 = reader2.ReadToEnd();
// but the wordML is saved correctly to xml file
doc.Save(@"C:\Documents and Settings\abdul.ali\Desktop\DocumentAliRTF.xml", SaveFormat.WordML);

Please advice.
Thank you,
Ali

Hi
Thanks for your request. Please try using the following code:

MemoryStream wmlStream = new MemoryStream();
// Save document
doc.Save(wmlStream, SaveFormat.WordML);
byte[] wmlBytes = wmlStream.GetBuffer();
// Get string from byte array
string wmlString = System.Text.Encoding.UTF8.GetString(wmlBytes, 0, (int)wmlStream.Length);
// Now we can write wmlstring to file or do something else
Stream wmlFile = new FileStream(@"Test205\out.xml", FileMode.Create);
StreamWriter wmlWriter = new StreamWriter(wmlFile);
wmlWriter.Write(wmlString);
wmlWriter.Close();
wmlFile.Close();

Hope this helps.
Best regards.

Thank u very much alexey