How to Delete comments by all or a specific author

  • OpenXML SDK
  • Spire.Doc
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            DeleteComments("DeleteComments.docx","Gary zhang");

        }
        public static void DeleteComments(string fileName, string author = "")
        {
            // Get an existing Wordprocessing document.
            using (WordprocessingDocument document =
                WordprocessingDocument.Open(fileName, true))
            {
                // Set commentPart to the document WordprocessingCommentsPart, 
                // if it exists.
                WordprocessingCommentsPart commentPart =
                    document.MainDocumentPart.WordprocessingCommentsPart;

                // If no WordprocessingCommentsPart exists, there can be no 
                // comments. Stop execution and return from the method.
                if (commentPart == null)
                {
                    return;
                }

                // Create a list of comments by the specified author, or
                // if the author name is empty, all authors.
                List commentsToDelete =
                    commentPart.Comments.Elements().ToList();
                if (!String.IsNullOrEmpty(author))
                {
                    commentsToDelete = commentsToDelete.
                    Where(c => c.Author == author).ToList();
                }
                IEnumerable commentIds =
                    commentsToDelete.Select(r => r.Id.Value);

                // Delete each comment in commentToDelete from the 
                // Comments collection.
                foreach (Comment c in commentsToDelete)
                {
                    c.Remove();
                }

                // Save the comment part change.
                commentPart.Comments.Save();

                Document doc = document.MainDocumentPart.Document;

                // Delete CommentRangeStart for each
                // deleted comment in the main document.
                List commentRangeStartToDelete =
                    doc.Descendants().
                    Where(c => commentIds.Contains(c.Id.Value)).ToList();
                foreach (CommentRangeStart c in commentRangeStartToDelete)
                {
                    c.Remove();
                }

                // Delete CommentRangeEnd for each deleted comment in the main document.
                List commentRangeEndToDelete =
                    doc.Descendants().
                    Where(c => commentIds.Contains(c.Id.Value)).ToList();
                foreach (CommentRangeEnd c in commentRangeEndToDelete)
                {
                    c.Remove();
                }

                // Delete CommentReference for each deleted comment in the main document.
                List commentRangeReferenceToDelete =
                    doc.Descendants().
                    Where(c => commentIds.Contains(c.Id.Value)).ToList();
                foreach (CommentReference c in commentRangeReferenceToDelete)
                {
                    c.Remove();
                }

                // Save changes back to the MainDocumentPart part.
                doc.Save();
            }
        }

    }

class Program
    {
        static void Main(string[] args)
        {
            DeleteComments("DeleteComments.docx", "Gary zhang");
        }
        public static void DeleteComments(string fileName, string authorName)
        {
            Document spireDoc = new Document();
            spireDoc.LoadFromFile(fileName);
            if (authorName == "")
            {
                spireDoc.Comments.Clear();
            }
            else
            {
                for (int i = 0; i < spireDoc.Comments.Count; i++)
                {
                    if (spireDoc.Comments[i].Format.Author == authorName)
                    {
                        spireDoc.Comments.Remove(spireDoc.Comments[i]);
                    }
                }
            }
            spireDoc.SaveToFile(fileName, FileFormat.Docx);
        }
    }