-1

I have some rtf content which is stored in the database as varchar. They originated from RichTextBoxes.

I want to concatenate the rtf content and save them as a Word document. How can I do this?

I found that the RichTextBox class has a SaveFile method which I can use to save a DOCX:

objRichTextBox.SaveFile("Temp.docx", RichTextBoxStreamType.RichText);

However a problem occurs when I try to add RTF content to the RichTextBox:

            RichTextBox objRichTextBox = new RichTextBox();           
            objRichTextBox.Rtf = objRichTextBox.Rtf + "Hello";

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

Additional information: File format is not valid.

What am I doing wrong?

Zesty
  • 2,922
  • 9
  • 38
  • 69
  • seems similar to http://stackoverflow.com/questions/4903304/invoking-word-for-rtf-to-docx-conversion – user1666620 May 18 '15 at 13:12
  • No, it's completely different. I neither have a saved RTF file somewhere nor do I have a RichTextBox on a form. When I just declare one and try to use it, it gives the error above. – Zesty May 18 '15 at 13:16
  • 3
    You can't change `Rtf` property of `RichTextBox` in this manner since it contains data in `rtf` format, and appending "Hello" to it breaks format integrity. – Andrey Korneyev May 18 '15 at 13:18
  • +1 Thanks, Andy. I now understand the error. But how can I save the RTF content as a .docx? I can't open an existing RTF document, as I don't have a saved RTF file. – Zesty May 18 '15 at 13:21
  • Okay, I found an actual question that is similar, now that I understand the problem, thanks to @[Andy Korneyev]. – Zesty May 18 '15 at 13:33

1 Answers1

0

Check this open-source OpenXMLWriter tool that shows how to generate DocX based on the content of Richtext control.

Or you may invoke MS Word to convert RTF file to DocX.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Eugene
  • 2,820
  • 19
  • 24