-2

I'm creating one winforms desktop application in c# that load .docx file. I want to load .docx file into RichTextBox. But when I'm trying to load .docx file, the format of that file is not getting correct. is there any other control or method to load and save .docx file with correct document format?

  • 1
    Possible duplicate of [Previewing .doc, .docx in C# Windows Forms](https://stackoverflow.com/questions/7787658/previewing-doc-docx-in-c-sharp-windows-forms) – Sinatr Sep 27 '19 at 11:42
  • It was the first duplicate, there are [more](https://www.google.com/search?q=winforms+docx+c%23+site:stackoverflow.com). Check [this one](https://stackoverflow.com/q/28521086/1997232). – Sinatr Sep 27 '19 at 12:43
  • @Sinatr its all shows for display/preview - not for edit in richTextBox. – Pritee Mehta Sep 28 '19 at 18:00
  • @PriteeMehta those formats aren't RTF. You can't use RichTextBox to display those documents, much less edit them. Read the duplicates - you'll need specialized controls for this or control Word through interop – Panagiotis Kanavos Oct 01 '19 at 09:14
  • @PriteeMehta as for faithful *rendering*, you definitely can't get that with an RTF control. Word documents have many features that aren't found in RTF – Panagiotis Kanavos Oct 01 '19 at 09:20

2 Answers2

0

Use this to read the file text and add the returned string to the Richtextbox.

 private string GetWordFileText(string filepath)
        {
            Microsoft.Office.Interop.Word.ApplicationClass WordApp = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            try
            {
                WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                doc = WordApp.Documents.Open(filepath, Visible: false);
                string toReturn = doc.Content.Text;
                return toReturn;
            }
            catch (Exception e)
            { throw e; }
            finally
            {
                doc.Close();
                WordApp.Quit();
            }
        }

After this comes the document styling. I don't have a code snippet handy for this but it would technically work as follows:

var formatting = Dictionary<string, Style>();
foreach(Paragraph para in doc.Paragraphs)
{
formatting.Add(para.Range.Text, (Style) para.getStyle());
}

Then inside the RichTextbox control you should figure out a method to apply styling

foreach(var fItem in formatting)
{
ApplyStyle(richTextBox, fItem.Key, fItem.Value);
}

void ApplyStyle(RichTextBox tb, string toFormat, Style style)
{
tb.SelectionFont = new Font(style.Font.Name, style.Font.Size);
tb.SelectedText = toFormat;
}
demoncrate
  • 390
  • 2
  • 14
  • can you please help me out how to apply style in richTextBox's text property ? – Pritee Mehta Sep 28 '19 at 17:44
  • I added code in the ApplyStyle method. Please try it out yourself and update this post with an answer once you get it to work. – demoncrate Oct 01 '19 at 09:10
  • This answer doesn't edit the Word documents. It only copies the text between Word and RTF. A Word document has a lot more features than RTF and simply can't be used like this – Panagiotis Kanavos Oct 01 '19 at 09:16
  • True, it is just not going to be possible to complete the task 100%. Displaying a rudimentary version of the document should work though. If editing were not required by the OP a PDF snapshot of the word document would suffice. – demoncrate Oct 01 '19 at 10:00
0

Finally, I got an answer. For load, Edit, Display - You can use RichEditControl of DexExpress tool, It comes up with all features of Word and also you can load RTF, .doc, .docx files in this control and edit it.