How to Insert a comment into a word processing document

  • OpenXML SDK
  • Spire.Doc
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            AddCommentOnFirstParagraph("Word8.docx","Open", "test", "This is my comment.");
        }
        // Insert a comment on the first paragraph.
        public static void AddCommentOnFirstParagraph(string fileName,
            string author, string initials, string comment)
        {
            // Use the file name and path passed in as an 
            // argument to open an existing Wordprocessing document. 
            using (WordprocessingDocument document =WordprocessingDocument.Open(fileName, true))
            {
                // Locate the first paragraph in the document.
                Paragraph firstParagraph =
                    document.MainDocumentPart.Document.Descendants().First();
                Comments comments = null;
                string id = "0";

                // Verify that the document contains a 
                // WordProcessingCommentsPart part; if not, add a new one.
                if (document.MainDocumentPart.GetPartsCountOfType() > 0)
                {
                    comments =
                        document.MainDocumentPart.WordprocessingCommentsPart.Comments;
                    if (comments.HasChildren)
                    {
                        // Obtain an unused ID.
                        id = comments.Descendants().Select(e => e.Id.Value).Max();
                    }
                }
                else
                {
                    // No WordprocessingCommentsPart part exists, so add one to the package.
                    WordprocessingCommentsPart commentPart =
                        document.MainDocumentPart.AddNewPart();
                    commentPart.Comments = new Comments();
                    comments = commentPart.Comments;
                }

                // Compose a new Comment and add it to the Comments part.
                Paragraph p = new Paragraph(new Run(new Text(comment)));
                Comment cmt =
                    new Comment()
                    {
                        Id = id,
                        Author = author,
                        Initials = initials,
                        Date = DateTime.Now
                    };
                cmt.AppendChild(p);
                comments.AppendChild(cmt);
                comments.Save();

                // Specify the text range for the Comment. 
                // Insert the new CommentRangeStart before the first run of paragraph.
                firstParagraph.InsertBefore(new CommentRangeStart()
                { Id = id }, firstParagraph.GetFirstChild());

                // Insert the new CommentRangeEnd after last run of paragraph.
                var cmtEnd = firstParagraph.InsertAfter(new CommentRangeEnd()
                { Id = id }, firstParagraph.Elements().Last());

                // Compose a run with CommentReference and insert it.
                firstParagraph.InsertAfter(new Run(new CommentReference() { Id = id }), cmtEnd);
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            AddCommentOnFirstParagraph("Word8.docx", "Open", "test", "This is my comment.");
        }
        public static void AddCommentOnFirstParagraph(string fileName, string author, string initials, string commentStr)
        {
            Document doc = new Document();
            doc.LoadFromFile(fileName);
            Spire.Doc.Section section = doc.Sections[0];
            Paragraph paragraph = section.Paragraphs[0];
            Comment spireComment = paragraph.AppendComment(commentStr);
            spireComment.Format.Author = author;
            spireComment.Format.Initial = initials;
            doc.SaveToFile(fileName, Spire.Doc.FileFormat.Docx);
        }
    }