Ron Miller
asked on
Formatting rich text
Hi there, I am using C# wpf and trying to operate on a rich text finding and replacing words and formatting them in bold italics etc by using code behind. What is the simplest way to do it as I have read many complicated options and can't decide on one. Thanks.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window
The extension method is below:
Open in new window
If you haven't used extension methods before, just save the above as a separate class file (I used Extensions.cs), and in your code-behind file where you want to use the SearchAndReplace() call, just add "using Extensions;" to the top of that file so that it can see that class.
NOTE: I'm certain there are some things that could be tweaked/optimized a bit in here (like the part that finds the replaced text ranges), but once I got it working, I just saved it as-is in a library and reused it from that point on and it worked pretty well.
I also have not tested it with any complex documents.