Delete all the comments by an author from the slide

  • OpenXML SDK
  • Spire.Presentation
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"..\..\Documents\myppt6.pptx";
            string author = "MZ";
            DeleteCommentsByAuthorInPresentation(fileName, author);
        }
        // Remove all the comments in the slides by a certain author.
        public static void DeleteCommentsByAuthorInPresentation(string fileName, string author)
        {
            if (String.IsNullOrEmpty(fileName) || String.IsNullOrEmpty(author))
                throw new ArgumentNullException("File name or author name is NULL!");

            using (PresentationDocument doc = PresentationDocument.Open(fileName, true))
            {
                // Get the specifed comment author.
                IEnumerable commentAuthors =
                    doc.PresentationPart.CommentAuthorsPart.CommentAuthorList.Elements()
                    .Where(e => e.Name.Value.Equals(author));

                // Iterate through all the matching authors.
                foreach (CommentAuthor commentAuthor in commentAuthors)
                {
                    UInt32Value authorId = commentAuthor.Id;

                    // Iterate through all the slides and get the slide parts.
                    foreach (SlidePart slide in doc.PresentationPart.SlideParts)
                    {
                        SlideCommentsPart slideCommentsPart = slide.SlideCommentsPart;
                        // Get the list of comments.
                        if (slideCommentsPart != null && slide.SlideCommentsPart.CommentList != null)
                        {
                            IEnumerable commentList =
                                slideCommentsPart.CommentList.Elements().Where(e => e.AuthorId == authorId.Value);
                            List comments = new List();
                            comments = commentList.ToList();

                            foreach (Comment comm in comments)
                            {
                                // Delete all the comments by the specified author.

                                slideCommentsPart.CommentList.RemoveChild(comm);
                            }

                            // If the commentPart has no existing comment.
                            if (slideCommentsPart.CommentList.ChildElements.Count == 0)
                                // Delete this part.
                                slide.DeletePart(slideCommentsPart);
                        }
                    }
                    // Delete the comment author from the comment authors part.
                    doc.PresentationPart.CommentAuthorsPart.CommentAuthorList.RemoveChild(commentAuthor);
                }
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            string fileName =@"..\..\Documents\myppt6.pptx";
            string author = "MZ";
            DeleteCommentsByAuthorInPresentation(fileName, author);
        }
        public static void DeleteCommentsByAuthorInPresentation(string fileName, string author)
        {
            if (String.IsNullOrEmpty(fileName) || String.IsNullOrEmpty(author))
                throw new ArgumentNullException("File name or author name is NULL!");

            //Instantiate a Presentation class
            using (Presentation pres = new Presentation(fileName, FileFormat.Pptx2010))
            {
                ICommentAuthor[] authors = pres.CommentAuthors.FindByName(author);
                ICommentAuthor thisAuthor = authors[0];
                for (int i = thisAuthor.CommentsList.Count - 1; i >= 0; i--)
                {
                    thisAuthor.CommentsList.RemoveAt(i);
                }
                //Save PPTX on disk
                pres.SaveToFile(@"..\..\Documents\result.pptx", FileFormat.Pptx2010);
            }
        }
    }