Tell me how in richTextBox to display the contents of files with the extension doc and docx? With txt files, everything is fine, but when opening doc, krakozyabry, like PK, will come out. The task is that there is a task to write a program for keeping a diary. Accordingly, it is necessary to make it possible to open already saved records with all the contents: text formatting, pictures, and so on. I want to display all this content, respectively, in a richTextBox.

private void открытьToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog o = new OpenFileDialog(); o.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*|*.doc|"; if (o.ShowDialog() == DialogResult.OK) { richTextBox1.Text = File.ReadAllText(o.FileName, Encoding.Default); } } 
  • Do not forget the WinForms label, even though your code shows it, but nonetheless. - rdorn
  • one
    A similar question has already been . - BlackWitcher
  • The docx file must either be opened with a vague library or converted to txt - nick_n_a
  • Do not tell me the name of the library? - Andrey
  • 2
    There is no easy way out. Otherwise, free analogs of Word would have been long ago. - Alexander Petrov

3 answers 3

Taking into account the clarification of the problem being solved, I can advise you to simply work with the native RichTextBox RTF format. There are enough examples of creating formatted text in MSDN, and here, and in the network in principle. Word and LibreOffice can open and save RTF, so your files will not only open in your program. When you can realize this, you can begin to adapt to modern document formats, if you have time and desire.

  • That is, if I save the tables and pictures in an RTF format document, will they open without problems? - Andrei
  • @ Andrei yes. RTF is a very old format, for it even has a built-in WordPad. Although in spite of age - the format is still alive. Sometimes it is more convenient than heavy modern markup formats, although of course it’s not clumsy =) - rdorn
  • Well, for starters, you can use it. Thanks a lot for your help. Half a day I sit on this task. - Andrey
  • @Andrew, don’t forget to mark the most correct question from your point of view with a green check mark, this will enable other visitors to quickly understand what exactly of the suggestions helped you. Even if a more correct or appropriate answer appears afterwards, it can always be rearranged. - rdorn
  • And in order to correctly save the image to a file, you need to dance with a tambourine? Or the usual code to write to the file? - Andrey

Give yourself the trouble to at least try to search in English: load word file (.docx) in richtextbox

I copy the code from there:

  if (openFile.ShowDialog() == true) { // Open document string originalfilename = System.IO.Path.GetFullPath(openFile.FileName); if (openFile.CheckFileExists && new[] { ".docx", ".doc", ".txt", ".rtf" }.Contains(Path.GetExtension(originalfilename).ToLower())) { Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application(); object File = originalfilename; object nullobject = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application(); wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject); docs.ActiveWindow.Selection.WholeStory(); docs.ActiveWindow.Selection.Copy(); rtfMain.Document.Paste(); docs.Close(ref nullobject, ref nullobject, ref nullobject); wordobject.Quit(ref nullobject, ref nullobject, ref nullobject); MessageBox.Show("file loaded"); } } 

Maybe this will not be the perfect result, which will completely satisfy you, but with it your question will not be so superficial.

  • Thank you very much. My technical English is not good enough and I don’t always understand if I have found exactly what I need. - Andrey

As already mentioned above, conversion to RTF format is necessary.

DOC - Binary format, with a very complex structure. Libraries that do not require an office to work with it sell for a lot of money. There are a couple of free. One open source is a port from Java POI. The second Spire DOC, has a bunch of restrictions on the amount of text processed.

DOCX - Archive with a set of XML files. There is a library from Microsoft OpenXML, which does an excellent job with docx files. But it is a bit complicated for development and most often use a shell over it - ClosedXML. Both libraries are free and do not require an established office. Or you can use a bunch of other free docx libraries. All of them are easily put through nuget.

COM Interop, described above, is ideal if the office is worth it. It "out of the box" supports all formats and perfectly converts.

If the office may be absent, then I would advise using either a POI or a bunch of POI / OpenXML.

And we must remember that RTF is very different from DOC / DOCX, and the result may differ from what you see in MS Word. Therefore, if the goal is to view documents, I would advise converting to HTML.