Hi,
I have attached the code to replicate this issue. Please run the attached code and convert the “test.xls” file in the common folder. As you will see the first two comments are incorrectly mapped to the same word in the document. A comment should be mapped to each “color” referenced in the test.xls file. But, “white” comment is incorrectly mapped to the color “orange” word. The comment “Orange” correctly maps to the word “Orange”.
Please let me know if there is a resolution to this issue.
Thanks.
Todd
Hi Todd,
Thanks for your request. The problem is in your ReplacingCallback. I modified it a bit. Please try using the following code:
public class ReplaceEvaluatorFindAndComment: IReplacingCallback
{
private NodeRange NR = null;
private Document Doc;
private string Color;
public ReplaceEvaluatorFindAndComment(NodeRange nodeRange, Document doc, string color)
{
NR = nodeRange;
Doc = doc;
Color = color;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.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 (e.MatchOffset> 0)
currentNode = SplitRun((Run) currentNode, e.MatchOffset);
// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while ((remainingLength> 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = 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(currentNode);
}
// Insert a comment.
Aspose.Words.Comment comment = new Aspose.Words.Comment(Doc, "xyx", string.Empty, System.DateTime.Now);
comment.SetText(Color);
CommentRangeStart start = new CommentRangeStart(Doc, comment.Id);
CommentRangeEnd end = new CommentRangeEnd(Doc, comment.Id);
Run firstRun = (Run) runs[0];
Run lastRun = (Run) runs[runs.Count - 1];
firstRun.ParentNode.InsertBefore(comment, firstRun);
firstRun.ParentNode.InsertBefore(start, firstRun);
lastRun.ParentNode.InsertAfter(end, firstRun);
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run) run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}
Hope this helps.
Best regards,