3

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.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
etr
  • 55
  • 1
  • 3

2 Answers2

5

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();
    }
}
Tommy
  • 131
  • 1
0

It depends on whether you want to count inherited bold/italic from styles or are just interested in inline bold/italic tags.

Tommy
  • 131
  • 1