在 c# 中使用 openxml sdk 检测 Word 文档中的粗体、下划线文本和标题

本文关键字:下划线 文本 标题 Word openxml sdk 检测 文档 | 更新日期: 2023-09-27 18:34:09

我想在选定的word文档中使用C#中的openxml sdk检测不同的样式和标题。这是我所拥有的:

 public string getWordDescription(string path)
        {
            string text = "";
            System.IO.Packaging.Package wordPackage = Package.Open(path, FileMode.Open, FileAccess.Read);
            using (WordprocessingDocument wordDocument =
WordprocessingDocument.Open(wordPackage))
            {              
                Body body = wordDocument.MainDocumentPart.Document.Body;
                if (body != null)
                {
                    foreach (Paragraph par in body.Descendants<Paragraph>())
                    {                                              
                        text += par.InnerText;
                        text += "<br />";                                              
                    }
                }
            }
            return text;
        }

因此,在循环播放段落时,我想以某种方式检测是否应用了一些样式或段落是否在标题。

在 c# 中使用 openxml sdk 检测 Word 文档中的粗体、下划线文本和标题

请记住,段落是块,块由运行组成。 因此,在 foreach 循环中,您需要一个内部循环来循环运行。 运行元素容器 描述内联格式的 RunProperty 元素。 或者我相信您只能使用如下所示的一个循环

foreach (Run run in body.Descendants<Run>())
{
   RunProperties props = run.Descendants<RunProperties>();
   if(props.Descendants<Bold>().First() != null) 
   {
      //then do something
   }
   text += run.Text;
   text += "<br />";                                              
}

试试这个

private RunStyles checkForEmphasis(Paragraph pTag)
    {
        bool isItalic = false;
        bool isBold = false;
        bool isUnderline = false;
        foreach (Run r in pTag.Descendants<Run>())
        {
            if (r.RunProperties != null)
            {
                RunProperties rProp = r.RunProperties;
                if (rProp.Italic != null)
                {
                    int x;
                    isItalic = true;
                }
                else
                {
                    isItalic = false;
                }
                if (rProp.Bold != null)
                {
                    isBold = true;
                }
                else
                {
                    isBold = false;
                }
                if (rProp.Underline != null)
                {
                    isUnderline = true;
                }
                else
                {
                    isUnderline = false;
                }
            }
            else
            {
                isItalic = false;
                isBold = false;
                isUnderline = false;
            }
        }
        if (isItalic && isBold && isUnderline)
        {
            return RunStyles.AllStyles;
        }
        else if (isBold && isItalic)
        {
            return RunStyles.BoldItalic;
        }
        else if (isBold && isUnderline)
        {
            return RunStyles.BoldUnderLine;
        }
        else if (isItalic && isUnderline)
        {
            return RunStyles.ItalicUnderLine;
        }
        else if (isItalic)
        {
            return RunStyles.Italic;
        }
        else if (isBold)
        {
            return RunStyles.Bold;
        }
        else if (isUnderline)
        {
            return RunStyles.UnderLine;
        }
        else
        {
            return RunStyles.NoStyle;
        }
    }

枚举运行样式

enum RunStyles
{
    AllStyles = 1,
    BoldItalic = 2,
    BoldUnderLine = 3,
    ItalicUnderLine = 4,
    Italic = 5,
    Bold = 6,
    UnderLine = 7,
    NoStyle = 0
}

基于此,您可以做自己的事情。这是完整的段落。

希望这有帮助。

//using MS word introp object   
 public static List<KeyValuePair<string, string>> GetBoldItalicUnderline(string strfilename, int style)//style=1 is bold and style=2 is italic and style=3
        {
            Microsoft.Office.Interop.Word.Application Objword = new Microsoft.Office.Interop.Word.Application();
            List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();
            //creating the object of document class  
            Document objdoc = new Document();
           // Document objdoc = Objword.Documents.Add();
            try
            {
                dynamic FilePath = strfilename;
                dynamic NA = System.Type.Missing;
                objdoc = Objword.Documents.Open
                              (ref FilePath, ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA, ref NA,
                               ref NA, ref NA, ref NA
                               );
                if (style == 1)
                {
                    Microsoft.Office.Interop.Word.Range rng = objdoc.Content;
                    Microsoft.Office.Interop.Word.Find find = rng.Find;
                    find.Font.Bold = 1;
                    find.Format = true;
                    while (find.Execute())
                    {
                        data.Add(new KeyValuePair<string, string>(rng.Text, "Bold"));
                    }
                }
                if (style == 2)
                {
                    Microsoft.Office.Interop.Word.Range rngitalic = objdoc.Content;
                    Microsoft.Office.Interop.Word.Find finditalic = rngitalic.Find;
                    finditalic.Font.Italic = 1;
                    finditalic.Format = true;
                    while (finditalic.Execute())
                    {
                        data.Add(new KeyValuePair<string, string>(rngitalic.Text, "Italic"));
                    }
                }
                if (style == 3)
                {
                    //Simgle Underline
                    Microsoft.Office.Interop.Word.Range rngunderline = objdoc.Content;
                    Microsoft.Office.Interop.Word.Find findunderline = rngunderline.Find;
                    findunderline.Font.Underline = WdUnderline.wdUnderlineSingle;
                    findunderline.Format = true;
                    while (findunderline.Execute())
                    {
                        // data.Add(rngunderline.Text);
                        data.Add(new KeyValuePair<string, string>(rngunderline.Text, "wdUnderlineSingle"));
                    }
                    //Dash
                    Microsoft.Office.Interop.Word.Range rngunderlineDash = objdoc.Content;
                    Microsoft.Office.Interop.Word.Find findunderlineDash = rngunderlineDash.Find;
                    findunderlineDash.Font.Underline = WdUnderline.wdUnderlineDash;
                    findunderlineDash.Format = true;
                    while (findunderlineDash.Execute())
                    {
                        //data.Add(rngunderlineDash.Text);
                        data.Add(new KeyValuePair<string, string>(rngunderlineDash.Text, "wdUnderlineDash"));
                    }
                    //DashHeavy

Microsoft.Office.Interop.Word.Range rngunderlineDashHeavy = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDashHeavy = rngunderlineDashHeavy.Find;
                findunderlineDashHeavy.Font.Underline = WdUnderline.wdUnderlineDashHeavy;
                findunderlineDashHeavy.Format = true;
                while (findunderlineDashHeavy.Execute())
                {
                    //  data.Add(rngunderlineDashHeavy.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDashHeavy.Text, "wdUnderlineDashHeavy"));
                }
                //DashLong
                Microsoft.Office.Interop.Word.Range rngunderlineDashLong = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDashLong = rngunderlineDashLong.Find;
                findunderlineDashLong.Font.Underline = WdUnderline.wdUnderlineDashLong;
                findunderlineDashLong.Format = true;
                while (findunderlineDashLong.Execute())
                {
                    // data.Add(rngunderlineDashLong.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDashLong.Text, "wdUnderlineDashLong"));
                }
                //DashLongHeavy
                Microsoft.Office.Interop.Word.Range rngunderlineDashLonghv = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDashLonghv = rngunderlineDashLonghv.Find;
                findunderlineDashLonghv.Font.Underline = WdUnderline.wdUnderlineDashLongHeavy;
                findunderlineDashLonghv.Format = true;
                while (findunderlineDashLonghv.Execute())
                {
                    // data.Add(rngunderlineDashLonghv.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDashLonghv.Text, "wdUnderlineDashLongHeavy"));
                }
                //DotDash
                Microsoft.Office.Interop.Word.Range rngunderlineDotDash = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDotDash = rngunderlineDotDash.Find;
                findunderlineDotDash.Font.Underline = WdUnderline.wdUnderlineDotDash;
                findunderlineDotDash.Format = true;
                while (findunderlineDotDash.Execute())
                {
                    //data.Add(rngunderlineDotDash.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDotDash.Text, "wdUnderlineDotDash"));
                }
                //DotDashHeavy
                Microsoft.Office.Interop.Word.Range rngunderlineDotDashHv = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDotDashHv = rngunderlineDotDashHv.Find;
                findunderlineDotDashHv.Font.Underline = WdUnderline.wdUnderlineDotDashHeavy;
                findunderlineDotDashHv.Format = true;
                while (findunderlineDotDashHv.Execute())
                {
                    //data.Add(rngunderlineDotDashHv.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDotDashHv.Text, "wdUnderlineDotDashHeavy"));
                }
                //DotDotDash
                Microsoft.Office.Interop.Word.Range rngunderlineDotDotDash = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDotDotDash = rngunderlineDotDotDash.Find;
                findunderlineDotDotDash.Font.Underline = WdUnderline.wdUnderlineDotDotDash;
                findunderlineDotDotDash.Format = true;
                while (findunderlineDotDotDash.Execute())
                {
                    // data.Add(rngunderlineDotDotDash.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDotDotDash.Text, "wdUnderlineDotDotDash"));
                }
                //DotDotDashHeavy
                Microsoft.Office.Interop.Word.Range rngunderlineDotDotDashHv = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDotDotDashHv = rngunderlineDotDotDashHv.Find;
                findunderlineDotDotDashHv.Font.Underline = WdUnderline.wdUnderlineDotDotDashHeavy;
                findunderlineDotDotDashHv.Format = true;
                while (findunderlineDotDotDashHv.Execute())
                {
                    //data.Add(rngunderlineDotDotDashHv.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDotDotDashHv.Text, "wdUnderlineDotDotDashHeavy"));
                }
                //Dotted
                Microsoft.Office.Interop.Word.Range rngunderlineDotted = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDotted = rngunderlineDotted.Find;
                findunderlineDotted.Font.Underline = WdUnderline.wdUnderlineDotted;
                findunderlineDotted.Format = true;
                while (findunderlineDotted.Execute())
                {
                    // data.Add(rngunderlineDotted.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDotted.Text, "wdUnderlineDotted"));
                }
                //DottedHeavy
                Microsoft.Office.Interop.Word.Range rngunderlineDottedHv = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDottedHv = rngunderlineDottedHv.Find;
                findunderlineDottedHv.Font.Underline = WdUnderline.wdUnderlineDottedHeavy;
                findunderlineDottedHv.Format = true;
                while (findunderlineDottedHv.Execute())
                {
                    //data.Add(rngunderlineDottedHv.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDottedHv.Text, "wdUnderlineDottedHeavy"));
                }
                //DoubleUnderline
                Microsoft.Office.Interop.Word.Range rngunderlineDouble = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineDouble = rngunderlineDouble.Find;
                findunderlineDouble.Font.Underline = WdUnderline.wdUnderlineDouble;
                findunderlineDouble.Format = true;
                while (findunderlineDouble.Execute())
                {
                    // data.Add(rngunderlineDouble.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineDouble.Text, "wdUnderlineDouble"));
                }
                //Thick
                Microsoft.Office.Interop.Word.Range rngunderlineThick = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineThick = rngunderlineThick.Find;
                findunderlineThick.Font.Underline = WdUnderline.wdUnderlineThick;
                findunderlineThick.Format = true;
                while (findunderlineThick.Execute())
                {
                    // data.Add(rngunderlineThick.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineThick.Text, "wdUnderlineThick"));
                }
                //Wavy
                Microsoft.Office.Interop.Word.Range rngunderlineWavy = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineWavy = rngunderlineWavy.Find;
                findunderlineWavy.Font.Underline = WdUnderline.wdUnderlineWavy;
                findunderlineWavy.Format = true;
                while (findunderlineWavy.Execute())
                {
                    // data.Add(rngunderlineWavy.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineWavy.Text, "wdUnderlineWavy"));
                }
                //wavyDouble
                Microsoft.Office.Interop.Word.Range rngunderlineWavyDbl = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineWavyDbl = rngunderlineWavyDbl.Find;
                findunderlineWavyDbl.Font.Underline = WdUnderline.wdUnderlineWavyDouble;
                findunderlineWavyDbl.Format = true;
                while (findunderlineWavyDbl.Execute())
                {
                    //data.Add(rngunderlineWavyDbl.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineWavyDbl.Text, "wdUnderlineWavyDouble"));
                }
                //WavyHeavy
                Microsoft.Office.Interop.Word.Range rngunderlineWavyHv = objdoc.Content;
                Microsoft.Office.Interop.Word.Find findunderlineWavyHv = rngunderlineWavyHv.Find;
                findunderlineWavyHv.Font.Underline = WdUnderline.wdUnderlineWavyHeavy;
                findunderlineWavyHv.Format = true;
                while (findunderlineWavyHv.Execute())
                {
                    //  data.Add(rngunderlineWavyHv.Text);
                    data.Add(new KeyValuePair<string, string>(rngunderlineWavyHv.Text, "wdUnderlineWavyHeavy"));
                }
            }
            return data;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (objdoc != null)
            {
                ((_Document)objdoc).Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objdoc);
                objdoc = null;
            }
            Process[] processes = Process.GetProcessesByName("winword");
            foreach (var process in processes)
            {
                process.Close();
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }