Hi Team,
I want to find the particular heading in the document then I need to check the heading below the particular content. both the heading and content matched and do not need to add a comment.
if the heading below the content is not matched we need to add the comment.
Please find the below input and expected output.
Expeceted_output200.docx (17.0 KB)
Input_word_document200.docx (13.0 KB)
string description = "This is the Heading 3";
string heading = "Heading 3";
var listHeadings = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1);
foreach (Paragraph paragraph in listHeadings)
{
if (paragraph.ToString(SaveFormat.Text).Trim().Equals(heading, StringComparison.InvariantCultureIgnoreCase))
{
var commenttextTopMarginText = "This description is worng";
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);
paragraph.PrependChild(start);
paragraph.AppendChild(comment);
paragraph.AppendChild(end);
}
}
@Princeshivananjappa,
Here is the code you are looking for:
private void Logic(Document doc)
{
string description = "This is the Heading 3";
string heading = "Heading 3";
var listParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>();
Paragraph headingReference = null;
foreach (Paragraph paragraph in listParagraphs)
{
// Last iteration we found the heading
if (headingReference != null)
{
// This paragraph must not be heading 1 or it means there is no content
if (paragraph.ParagraphFormat.StyleIdentifier != StyleIdentifier.Heading1 &&
!paragraph.ToString(SaveFormat.Text).Trim().Equals(description, StringComparison.InvariantCultureIgnoreCase)) // ! or Negated or not equals
{
var commenttextTopMarginText = "This description is wrong";
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);
paragraph.PrependChild(start);
paragraph.AppendChild(comment);
paragraph.AppendChild(end);
}
//we break always after the heading was found
break;
}
if(paragraph.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1 &&
paragraph.ToString(SaveFormat.Text).Trim().Equals(heading, StringComparison.InvariantCultureIgnoreCase))
{
headingReference = paragraph;
continue;
}
}
}