Retrieve comments from a word processing document

OpenXML SDK

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenXML_SDK
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "Retrieve comments from word processing document.docx";
            GetCommentsFromDocument(fileName);
        }
        public static void GetCommentsFromDocument(string fileName)
        {
            using (WordprocessingDocument wordDoc =
                WordprocessingDocument.Open(fileName, false))
            {
                WordprocessingCommentsPart commentsPart =
                    wordDoc.MainDocumentPart.WordprocessingCommentsPart;

                if (commentsPart != null && commentsPart.Comments != null)
                {
                    foreach (Comment comment in commentsPart.Comments.Elements<Comment>())
                    {
                        Console.WriteLine(comment.InnerText);
                    }
                }
            }
        }
    }
}

Aspose.Words

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Words;
using System.Collections;
namespace Aspose_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document("Retrieve comments from word processing document.docx");
            ExtractComments(doc);

        }
        public static void ExtractComments(Document doc)
        {
            ArrayList collectedComments = new ArrayList();
            // Collect all comments in the document
            NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);
            // Look through all comments and gather information about them.
            foreach (Comment comment in comments)
            {
                collectedComments.Add(comment.Author + " " + comment.DateTime + " " + comment.ToString(SaveFormat.Text));
            }
            foreach (string collectedComment in collectedComments)
            {
                Console.WriteLine(collectedComment);
            }

        }
    }
}

Download

Last edited Jan 24, 2014 at 10:39 AM by asposemarketplace, version 2