etr
etr

Reputation: 55

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.

Upvotes: 3

Views: 7518

Answers (2)

Tommy
Tommy

Reputation: 131

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

Upvotes: 5

Tommy
Tommy

Reputation: 131

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

Upvotes: 0

Related Questions