Search code examples
openxmlvisual-studio-power-tools

OpenXml - iterate through a paragraph's runs and find if a run has italic or bold text


I am trying to iterate through paragraph runs, find if a run has italized/bold text and replace that text with something else.

Which is the best method in terms of performance.


Solution

  • If you are interested only in inline tags, the following code can help. Just change the Convert() method to whatever you want.

    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    class Program
    {
        static void Main(string[] args)
        {
            using (var doc = WordprocessingDocument.Open(@"c:\doc1.docx", true))
            {
                foreach (var paragraph in doc.MainDocumentPart.RootElement.Descendants<Paragraph>())
                {
                    foreach (var run in paragraph.Elements<Run>())
                    {
                        if (run.RunProperties != null &&
                            (run.RunProperties.Bold != null && (run.RunProperties.Bold.Val == null || run.RunProperties.Bold.Val) ||
                            run.RunProperties.Italic != null && (run.RunProperties.Italic.Val == null || run.RunProperties.Italic.Val)))
                            Process(run);
                    }
                }
            }
        }
    
        static void Process(Run run)
        {
            string text = run.Elements<Text>().Aggregate("", (s, t) => s + t.Text);
            run.RemoveAllChildren<Text>();
            run.AppendChild(new Text(Convert(text)));
    
        }
    
        static string Convert(string text)
        {
            return text.ToUpper();
        }
    }