I am trying to add the comment for the paragraph

@alexey.noskov I am trying to add the comment for the paragraph I used below step.

  1. Find the particular heading 1
    2.next I am checking the table is there or not
  2. I am checking particular paragraph is there or not. if the particular text is there means I am add the comment for it.

Note: my heading one having the comment that’s why my 1st condition having not satisfying.so i want to ignore the comment for heading 1

Kindly help me asap.

Please find the below mentioned input and expected output.
Expeceted_output_document96.docx (48.6 KB)
Input_word document 123.docx (48.4 KB)

string description = "* Process owner/Service Owner communicates to HR Training Coordinator of any additional roles/colleagues who are required to train on the process document who do not fall under the defined departments/groups.";
string heading = "Training Audience";
var listParagraphs = ruleBaseModel.SourceDocument.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();

foreach (Paragraph paragraph in listParagraphs)
{
    var abc = paragraph.ToString(SaveFormat.Text).Trim();

    if (paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 &&
        abc == heading.Trim())
    {
        Table table = paragraph.NextSibling as Table;
        if (table != null)
        {
            Node nextNode = table.NextSibling;
            while (nextNode != null && nextNode.NodeType != NodeType.Paragraph)
                nextNode = nextNode.NextSibling;

            Paragraph paragraph1 = (Paragraph)nextNode;

            // This paragraph must not be heading 1 or it means there is no content
            if (paragraph1.ParagraphFormat.StyleIdentifier != StyleIdentifier.Heading1 &&
                    !paragraph1.ToString(SaveFormat.Text).Trim().Equals(description, StringComparison.InvariantCultureIgnoreCase)) // ! or Negated or not equals
            {
                var commenttextTopMarginText = "This description is null";
                Comment comment = new Comment(ruleBaseModel.SourceDocument, "mmh", "007", DateTime.Today);
                comment.Paragraphs.Add(new Paragraph(ruleBaseModel.SourceDocument));
                comment.FirstParagraph.AppendChild(new Run(ruleBaseModel.SourceDocument, commenttextTopMarginText));
                CommentRangeStart start = new CommentRangeStart(ruleBaseModel.SourceDocument, comment.Id);
                CommentRangeEnd end = new CommentRangeEnd(ruleBaseModel.SourceDocument, comment.Id);

                paragraph1.PrependChild(start);
                paragraph1.AppendChild(comment);
                paragraph1.AppendChild(end);
            }
            break;
        }
    }
}

@Princeshivananjappa,

While I work on an answer I will let you know why the following line does not work:

var textParagraph = paragraph.ToString(SaveFormat.Text).Trim();

The line above will not work because it adds the whole paragraph, including the comments, so the content is actually “Training AudienceThis is the I found”.
Which are the paragraph and the comment together.

I will post a solution soon.

@Princeshivananjappa,

The following code will help you with your request.

private void Logic(Document doc)
{
    string descriptionForTableToFind = "*Process Owner/Service Owner communicates to HR Training Coordinator any additional roles/colleagues hello.";
    string headingToFind = "Training Audience";
    var listParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();

    foreach (Paragraph paragraph in listParagraphs)
    {
        if (paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
        {                    
            paragraph.JoinRunsWithSameFormatting();
            foreach (Run runHeading in paragraph.Runs)
            {
                if (runHeading.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (paragraph.NextSibling is Table) // The next node is a table
                    {
                        var tableSibling = paragraph.NextSibling;
                        //We loop the paragraphs until we find the next Paragraph
                        while (tableSibling.NextSibling != null && !(tableSibling.NextSibling is Paragraph))
                        {
                            tableSibling = tableSibling.NextSibling;
                        }

                        Paragraph paragraphSibling = tableSibling.NextSibling as Paragraph;

                        // This paragraph must not be heading 1 or it means there is no content
                        if (paragraphSibling != null && paragraphSibling.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                            break;

                        paragraphSibling.JoinRunsWithSameFormatting();
                        foreach (Run runContent in paragraphSibling.Runs)
                        {
                            if (runContent.Text.Equals(descriptionForTableToFind, StringComparison.InvariantCultureIgnoreCase))
                            {
                                // Your logic to add the comment.


                                var commenttextTopMarginText = "This description is null";
                                Comment comment = new Comment(doc, "mmh", "007", DateTime.Today);
                                comment.Paragraphs.Add(new Paragraph(doc));
                                comment.FirstParagraph.AppendChild(new Run(doc, commenttextTopMarginText));
                                CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
                                CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

                                paragraphSibling.PrependChild(start);
                                paragraphSibling.AppendChild(comment);
                                paragraphSibling.AppendChild(end);
                            }
                        }
                    }
                }
            }                    
        }                
    }
}

The input and output files:
ParagraphWithTableAddingComment_input.docx (48.4 KB)
ParagraphWithTableAddingComment_output.docx (45.2 KB)

@carlosmc Your given code is comparing only starting line .its not comparing ending of the line. kindly help me.
please find the below mentioned screen short
image.png (83.3 KB)

@Princeshivananjappa,

You have to remember that word documents are free flow, so there is no end of the line. You will have to come up with your own logic to compare. I am comparing the contents in the runs with the same format
(But you removed that line in your code as is shown in your picture).
If you modify the logic, the result wont be the same. You can have runs with different formats and modify that code according to your own criteria. But that is something for you to decide and code depending in your requirements.

@carlosmc I used same code what you given I just modifying content not equals. but i am getting only staring of the string.
one more thing below code is adding 4 times same comment for content.
and we are not modifying anything in other heading content but its re-writing why?
Please find the below screen short.
image.png (57.7 KB)

public static void Logic(Document doc)
{
    string descriptionForTableToFind = "*Process Owner/Service Owner communicates to HR Training Coordinator any additional roles/colleagues hello.";
    string headingToFind = "Training Audience";
    var listParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();

    foreach (Paragraph paragraph in listParagraphs)
    {
        if (paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
        {
            paragraph.JoinRunsWithSameFormatting();
            foreach (Run runHeading in paragraph.Runs)
            {
                if (runHeading.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (paragraph.NextSibling is Table) // The next node is a table
                    {
                        var tableSibling = paragraph.NextSibling;
                        //We loop the paragraphs until we find the next Paragraph
                        while (tableSibling.NextSibling != null && !(tableSibling.NextSibling is Paragraph))
                        {
                            tableSibling = tableSibling.NextSibling;
                        }

                        Paragraph paragraphSibling = tableSibling.NextSibling as Paragraph;

                        // This paragraph must not be heading 1 or it means there is no content
                        if (paragraphSibling != null && paragraphSibling.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                            break;

                        paragraphSibling.JoinRunsWithSameFormatting();
                        foreach (Run runContent in paragraphSibling.Runs)
                        {
                            if (!runContent.Text.Equals(descriptionForTableToFind, StringComparison.InvariantCultureIgnoreCase))
                            {
                                // Your logic to add the comment.


                                var commenttextTopMarginText = "This description is null";
                                Comment comment = new Comment(doc, "mmh", "007", DateTime.Today);
                                comment.Paragraphs.Add(new Paragraph(doc));
                                comment.FirstParagraph.AppendChild(new Run(doc, commenttextTopMarginText));
                                CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
                                CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

                                paragraphSibling.PrependChild(start);
                                paragraphSibling.AppendChild(comment);
                                paragraphSibling.AppendChild(end);
                            }
                        }
                    }
                }
            }
        }
    }
}

@Princeshivananjappa,

You can clearly see in your picture that you commented the code of the following line:

paragraphSibling.JoinRunsWithSameFormatting();

Is in the image attached, so the code you are running and the code you are pasting is not the same.

@carlosmc Sorry for that.
Please find below code.

public static void Logic(Document doc)
{
    string descriptionForTableToFind = "*Process Owner/Service Owner communicates to HR Training Coordinator any additional roles/colleagues hello.";
    string headingToFind = "Training Audience";
    var listParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();

    foreach (Paragraph paragraph in listParagraphs)
    {
        if (paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
        {
            // paragraph.JoinRunsWithSameFormatting();
            foreach (Run runHeading in paragraph.Runs)
            {
                if (runHeading.Text.Equals(headingToFind, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (paragraph.NextSibling is Table) // The next node is a table
                    {
                        var tableSibling = paragraph.NextSibling;
                        //We loop the paragraphs until we find the next Paragraph
                        while (tableSibling.NextSibling != null && !(tableSibling.NextSibling is Paragraph))
                        {
                            tableSibling = tableSibling.NextSibling;
                        }

                        Paragraph paragraphSibling = tableSibling.NextSibling as Paragraph;

                        // This paragraph must not be heading 1 or it means there is no content
                        if (paragraphSibling != null && paragraphSibling.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                            break;

                        // paragraphSibling.JoinRunsWithSameFormatting();
                        foreach (Run runContent in paragraphSibling.Runs)
                        {
                            if (!runContent.Text.Equals(descriptionForTableToFind, StringComparison.InvariantCultureIgnoreCase))
                            {
                                // Your logic to add the comment.


                                var commenttextTopMarginText = "This description is null";
                                Comment comment = new Comment(doc, "mmh", "007", DateTime.Today);
                                comment.Paragraphs.Add(new Paragraph(doc));
                                comment.FirstParagraph.AppendChild(new Run(doc, commenttextTopMarginText));
                                CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
                                CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);

                                paragraphSibling.PrependChild(start);
                                paragraphSibling.AppendChild(comment);
                                paragraphSibling.AppendChild(end);
                            }
                        }
                    }
                }
            }
        }
    }
}

@Princeshivananjappa,

You will have to add your own custom logic to the comparison. You can loop throu all the runs and save it, in a variable and then do the comparison. But since this is something generic that you need to do(string comparison) You can do change that line for a function, with your own code and logic.

Thats why I used the “JoinRunsWithSameFormatting” method. You can replace that by your own function with your own code.