We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Associate a comment to a specific word when inserting HTML string

Hello,
I am inserting html content to a document and wanted to insert a comment to a specific word within the html content. I am not able to associate a comment with a specific place in word export.
Example
<p> This is an Html Text. <span>And</span> I wanted to add comment to it.</p>

I wanted to insert a comment to the word “And” when I export the html to word. I couldn’t find a documentation to implement this for insertHtml().

@tesfayemissy,

You can use the following code to insert a comment in Word document via InsertHtml method:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml("<div><p><span>This is an Html Text. </span><a name=\"_cmntref1\"><span>And</span></a><a href=\"#_cmnt1\"><span>[a1]</span></a><span> I wanted to add comment to it.</span></p></div><hr /><div id=\"_cmnt1\" style=\"-aw-comment-author:'awaishafeez'; -aw-comment-datetime:'2018-11-15T07:07:00'; -aw-comment-initial:'a'\"><p><a href=\"#_cmntref1\"><span>[a1]</span></a><span>Comment Text</span></p></div>");

doc.Save("D:\\temp\\18.11.docx"); 

Hope, this helps.

@awais.hafeez

Thank you for your quick response. The code you provided resolves my issue. Is there an attribute for comment reply or do I just append it as a regular comment?

@tesfayemissy,

Please ZIP and attach your expected DOCX document showing the correct output here for our reference. You can create expected document by using MS Word. We will then provide you HTML to achieve the same by using Aspose.Words.

@awais.hafeez

I uploaded the output i’m expecting . Associating multiple comments to one text and also associating the replies to the same text the parent comment refers to when implementing insertHtml().

Associating comments to a text.png (8.3 KB)

@tesfayemissy,

Please see these HTML and DOCX files (reply-comment.zip (7.6 KB)) and try running the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertHtml(File.ReadAllText("D:\\Temp\\reply-comment.html"));

doc.Save("D:\\temp\\18.11.docx");

Hope, this helps.

@awais.hafeez

That really helps. Thanks you for your quick response. One last question, can we find a word in an exiting word document and add comment to it ? For example if we take “18.11.docx” , if I wanted to add a comment to the word “Comment” in the generated word document, can we do that with Aspose?

@tesfayemissy,

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

Hope, this helps.

@awais.hafeez

Thank you for your quick response again. This should definitely help.