How to add the comment for heading 1.Kindly help me asap.
Please find the below input and expected output word document.
Expected_output10.docx (22.6 KB)
Input word document10.docx (23.0 KB)
@Princeshivananjappa Please see our documentation to learn how to work with comment. In your case you can use code like this to add comment to the paragraph in your document:
Document doc = new Document(@"C:\Temp\in.docx");
// Get heading paragraph.
Paragraph heading = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
.Where(p => (p.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)).FirstOrDefault();
// Create and add a comment.
Comment comment = new Comment(doc, "James Bond", "007", DateTime.Today);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.AppendChild(new Run(doc, "This is my cool comment!"));
CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);
heading.PrependChild(start);
heading.AppendChild(end);
heading.AppendChild(comment);
doc.Save(@"C:\Temp\out.docx");
@alexey.noskov Thank you for the quick response. I used the same code for a run but it’s thouing error like Run doesn’t contain a definition of PrependChild
. Kindly help me.
public static void comment(Document doc)
{
int value = 14;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
{
foreach (Run r in para.Runs)
{
if (r.Font.Size.ToString() != Convert.ToDouble(value).ToString())
{
var commenttextRightMarginText = "Heading1 Level1 Font [Font Size]: Old value:" + r.Font.Size + ", New value: " + Convert.ToDouble(value);
r.Font.Size = Convert.ToDouble(value);
// Create and add a comment.
Comment comment = new Comment(doc, "James Bond", "007", DateTime.Today);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.AppendChild(new Run(doc, commenttextRightMarginText));
CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);
r.PrependChild(start);
r.AppendChild(end);
r.AppendChild(comment);
}
}
}
}
}
@Princeshivananjappa Paragraph
is a composite node, while Run
is inline node and cannot contain children. So if you need to comment some Run
, you should insert comment nodes on the same level as Run
. For example see the following code:
// Create and add a comment.
Comment comment = new Comment(doc, "James Bond", "007", DateTime.Today);
comment.Paragraphs.Add(new Paragraph(doc));
comment.FirstParagraph.AppendChild(new Run(doc, "This is my cool comment!"));
CommentRangeStart start = new CommentRangeStart(doc, comment.Id);
CommentRangeEnd end = new CommentRangeEnd(doc, comment.Id);
// Use InsertBefore and InsettAfer methods to insert comment nodes.
run.ParentNode.InsertBefore(start, run);
run.ParentNode.InsertAfter(comment, run);
run.ParentNode.InsertAfter(end, run);
Please see our documentation to learn more about Aspose.Words Document Object Model.