c# – How do I find my input text is BOLD Typography?

问题内容:

I have uploaded a ms word file on my sever. After uploading the file I am reading that file and I want read BOLD words only. The thing is that I can able to find whether the file contains BOLD words or not. But I want to read that BOLD words.
thought the system says, this Paragraph contains a bold word. but I want to read only those bold words.

I have used MS office library to read word file.
Microsoft.Office.Interop.Word;

following is my code to detect BOLD words.

    foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
    {
   Microsoft.Office.Interop.Word.Range parRng = paragraph.Range;
     if (parRng.Bold > 0)
        {
         //  here i can able to detect this paragraph contains bold 
         //character but unable to read those specfic bold words
        }
    }

问题评论:

答案:

答案1:

Instead of iterating through Paragraphs, use Sentences. And futher you can iterate though each word to find out the Bold text.

using Microsoft.Office.Interop.Word;
using System;

namespace consolFindBoldWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = new Application();
            Document doc = application.Documents.Open("I:\\word.docx");

            foreach (Range s in doc.Sentences)
            {
                foreach (Range rg in s.Words)
                {
                    if (rg.Bold == -1)
                    {

                        /*  WRITE YOUR CODE HERE IF WORD IS BOLD*/
                        Console.WriteLine("Bold : {0}", rg.Text);
                    }
                }
            }

            doc.Close();
        }
    }
}

答案评论:

原文地址:

https://stackoverflow.com/questions/47749896/c-sharp-how-do-i-find-my-input-text-is-bold-typography

添加评论