Insert comment at specific character postion in word

Hi,
I have a word document and a character start and end position at which comments needs to be added. Comments can be nested or overlapped.
Is there any way to go to specific character postion in word and insert comment around it?

Any help would be appreciated.

Thanks,
Bipin

@bipih.oswal,

Please refer to the following article
Working with Comments
Hope, this helps.

I alrrady read this article but my requirement is slightly different.

I want to read paragraph text and i want toa add commnet at specific start and end position.

I am able yo calculate the position at which i want to add the comment but as is plain text i don’t know h8w to add commnet there.

To give you more details,i have a html and postion in html to which commnet needs to be added.
I tried to replace text in html which in such a way that aspose will understand and will direcly get converted to comment in word.
But it is getting very complicated if commmets are nested /overlapped or have replies. Simply text parsing gonna cause problems.

So i want to add comments using aspose api which is already handling all complexities.

@bipih.oswal,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input document
  • Aspose.Words generated output DOCX document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

I think there is some misunderstanding here.

My question is not about undesired behavior.
I am asking the question whether it is possible to add comments by iterating over paragraph text and add the comment at some start and end position?

@bipih.oswal,

Please see these input and output sample documents: add comment to text.zip (19.1 KB)

You can find text in Word document and add a comment to it by using the following code:

Document doc = new Document("D:\\temp\\input.docx");

FindReplaceOptions opts = new FindReplaceOptions();
opts.Direction = FindReplaceDirection.Backward;
opts.ReplacingCallback = new ReplaceEvaluator();

doc.Range.Replace(new Regex("Process me"), "", opts);

doc.Save("D:\\temp\\18.11-commented.docx");
//////////////////////////////////////////////
private class ReplaceEvaluator : IReplacingCallback
{
    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 removing.
        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);
        }


        Comment comment = new Comment(e.MatchNode.Document, "Awais Hafeez", "AH", DateTime.Today);
        comment.Paragraphs.Add(new Paragraph(e.MatchNode.Document));
        comment.FirstParagraph.Runs.Add(new Run(e.MatchNode.Document, "Comment text."));

        CommentRangeStart commentRangeStart = new CommentRangeStart(e.MatchNode.Document, comment.Id);
        CommentRangeEnd commentRangeEnd = new CommentRangeEnd(e.MatchNode.Document, comment.Id);

        Run runStart = (Run)runs[0];
        Run runEnd = (Run)runs[runs.Count - 1];

        runStart.ParentNode.InsertBefore(commentRangeStart, runStart);
        runEnd.ParentNode.InsertAfter(commentRangeEnd, runEnd);
        commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);

        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), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

Hope, this helps in achieving what you are looking for.