Wiki
Clone wikiAspose for OpenXML / Delete comments by all or a specific author
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) { DeleteComments(@"Delete comments by all or by an specific author.docx", ""); } // Delete comments by a specific author. Pass an empty string for the // author to delete all comments, by all authors. 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<Comment> commentsToDelete = commentPart.Comments.Elements<Comment>().ToList(); if (!String.IsNullOrEmpty(author)) { commentsToDelete = commentsToDelete. Where(c => c.Author == author).ToList(); } IEnumerable<string> 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<CommentRangeStart> commentRangeStartToDelete = doc.Descendants<CommentRangeStart>(). 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<CommentRangeEnd> commentRangeEndToDelete = doc.Descendants<CommentRangeEnd>(). 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<CommentReference> commentRangeReferenceToDelete = doc.Descendants<CommentReference>(). Where(c => commentIds.Contains(c.Id.Value)).ToList(); foreach (CommentReference c in commentRangeReferenceToDelete) { c.Remove(); } // Save changes back to the MainDocumentPart part. doc.Save(); } } } }
Aspose.Words
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aspose.Words; namespace Aspose_Word { class Program { static void Main(string[] args) { Document doc=new Document(@"Delete comments by all or by an specific author.docx"); RemoveComments(doc, ""); } public static void RemoveComments(Document doc, string authorName) { // Collect all comments in the document NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true); if (authorName == "") { // Remove all comments. comments.Clear(); } else { // Look through all comments and remove those written by the authorName author. for (int i = comments.Count - 1; i >= 0; i--) { Comment comment = (Comment)comments[i]; if (comment.Author == authorName) comment.Remove(); } } doc.Save(@"Delete comments by all or by an specific author.docx"); } } }
Download
Updated