How to List out Word Document Formats (ex: Bold, Italic, BoldItalic) using C#

Aberam Wijayakumar 21 Reputation points
2021-01-02T02:58:30.457+00:00

Let friends know if you have any idea. I need to list out all the formats in the word document using c#. I tried word to XML convert and then read XML file formats, but it's not working correctly. So can anyone help me?

I attached the picture below. I need list out like this.

52689-untitled.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
Office Management
Office Management
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Management: The act or process of organizing, handling, directing or controlling something.
2,026 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
901 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-01-04T05:37:16.33+00:00

    You can use Microsoft.Office.Interop.Word to do this, a simple example:

            private void browseBtn_Click(object sender, EventArgs e)  
            {  
                string filePath = null;  
                using (OpenFileDialog openFileDialog = new OpenFileDialog())  
                {  
                    openFileDialog.InitialDirectory = "d:\\";  
                    openFileDialog.Filter = @"All Files|*.txt;*.docx;*.doc;";  
      
                    openFileDialog.FilterIndex = 2;  
                    openFileDialog.RestoreDirectory = true;  
      
                    if (openFileDialog.ShowDialog() == DialogResult.OK)  
                    {  
                        //Get the path of specified file  
                        filePath = openFileDialog.FileName;  
      
                        //Read the contents of the file into a stream  
                        var fileStream = openFileDialog.OpenFile();  
                    }  
                }  
                Word.Application application = new Word.Application();  
                Document document = application.Documents.Open(filePath);  
                List<string> text = GeyText(document);  
                textBox1.Text = text[0];  
                textBox2.Text = text[1];  
                document.Close();  
                application.Quit();  
            }  
      
            private List<string> GeyText(Document document)  
            {  
                List<string> strings = new List<string>();  
                StringBuilder boldText = new StringBuilder();  
                StringBuilder italicText = new StringBuilder();  
                foreach (Range item in document.Words)  
                {  
                    if (item.Bold == -1)  
                    {  
                        boldText.Append(item.Text + Environment.NewLine);  
                    }  
                    if (item.Italic == -1)  
                    {  
                        italicText.Append(item.Text + Environment.NewLine);  
                    }  
                }  
                strings.Add(boldText.ToString());  
                strings.Add(italicText.ToString());  
      
                return strings;  
            }  
    

    53128-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-01-02T12:01:17.63+00:00
    0 comments No comments

  2. David 146 Reputation points
    2021-02-18T03:55:15.507+00:00

    Alternatively, you could try Spire.Doc to retrieve all style names used in a Word document. Below is the code snippet for your reference.

    using Spire.Doc;
    using Spire.Doc.Documents;
    using Spire.Doc.Fields;
    using System;
    using System.Text.RegularExpressions;
    namespace RetrieveStyleNames
    {
        class Program
        {
            static void Main(string[] args)
            {
                Document doc = new Document();
                doc.LoadFromFile("Sample.docx");
    
                foreach (Section section in doc.Sections)
                {
                    foreach (Paragraph paragraph in section.Paragraphs)
                    {
                        foreach (DocumentObject docObject in paragraph.ChildObjects)
                        {
                            if (docObject.DocumentObjectType == DocumentObjectType.TextRange)
                            {
                                TextRange text = docObject as TextRange;
                                Console.WriteLine(text.StyleName);
                            }
                        }
                        Console.WriteLine();
                    }
                }
            }
        }
    }
    
    0 comments No comments