c# - 如何找到输入文字是BOLD Typography?

时间:2017-12-11 09:36:19

标签: c# asp.net .net ms-office

我在服务器上上传了一个ms word文件。上传文件后,我正在阅读该文件,我只想阅读 BOLD 字样。问题是,我能够找到该文件是否包含 BOLD 字样。但我想阅读 BOLD 字样。  系统认为这个段落包含了一个粗体字。但我只想读那些大胆的话。

我使用MS office library来读取word文件。   的Microsoft.Office.Interop.Word;

以下是检测 BOLD 字词的代码。

    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 个答案:

答案 0 :(得分:1)

不使用段落迭代,而是使用Sentences。此外,您可以迭代每个单词以找出粗体文本。

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();
        }
    }
}