Hi,
I have a requirement to read the highlighted text from the paragraph, and I am able to do so via the run properties ‘run.Font.HighlightColor.IsEmpty’. but I would like to know if there is any other way where I can do it in the find and replace callback function with some regex pattern. I want to do it with regex because I am using regex find and replace to find other formatting issues like repeated words etc.
Thanks
@Nizzam2024 Unfortunately, there is no way to find highlighted text using regular expression. But you can check whether the captured text is highlighted. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions(FindReplaceDirection.Backward);
opt.ReplacingCallback = new CheckHighlightedCallback();
doc.Range.Replace("find me", "", opt);
private class CheckHighlightedCallback : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
List<Run> matchedRuns = GetMatchedRuns(args);
Console.WriteLine(matchedRuns.Any(r => !r.Font.HighlightColor.IsEmpty));
return ReplaceAction.Skip;
}
private static List<Run> GetMatchedRuns(ReplacingArgs args)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = args.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (args.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, args.MatchOffset);
// This array is used to store all nodes of the match for further deleting.
List<Run> runs = new List<Run>();
// Find all runs that contain parts of the match string.
int remainingLength = args.Match.Value.Length;
while (
remainingLength > 0 &&
currentNode != null &&
currentNode.GetText().Length <= remainingLength)
{
runs.Add((Run)currentNode);
remainingLength -= currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
} while (currentNode != null && currentNode.NodeType != NodeType.Run);
}
// Split the last run that contains the match if there is any text left.
if (currentNode != null && remainingLength > 0)
{
SplitRun((Run)currentNode, remainingLength);
runs.Add((Run)currentNode);
}
return runs;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
}
let me try
Thanks @alexey.noskov
1 Like