C#: Modify or Extract Comments in PowerPoint

In PowerPoint, comments are a very useful feature that can help you add notes or feedback to your slides. In addition to adding or removing comments, sometimes you may need to modify existing comments to correct errors or outdated information. Or in a collaborative editing scenario, you may need to extract comments to collect suggestions of all members. This article will demonstrate how to modify or extract comments in PowerPoint in C# using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Modify Comments on a Presentation Slide in C#

The ISlide.Comments[].Text property provided by Spire.Presentation for .NET allows you to update the content of a specified comment with new text. The following are the detailed steps.

  • Create a Presentation instance.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specified slide through Prenstion.Slides[] property.
  • Update a specified comment on the slide through ISlide.Comments[].Text property.
  • Save the result file using Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace ModifyComment
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation instance
            Presentation presentation = new Presentation();

            // Load a PowerPoint presentation
            presentation.LoadFromFile("Comments.pptx");

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Update the first comment in the slide
            slide.Comments[0].Text = "Replace comment";

            // Save the result file
            presentation.SaveToFile("ModifyComment.pptx", FileFormat.Pptx2016);

        }
    }
}

Modify the first comment on the first slide with new text

Extract Comments from a Presentation Slide in C#

To extract comments from a slide, you need to get all the comments in the slide via the ISlide.Comments property, and then iterate through each comment to get its text content via the Comment.Text property. The following are the detailed steps.

  • Create a Presentation instance.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Create a StringBuilder instance.
  • Get a specified slide through Prenstion.Slides[] property.
  • Get all comments in the slide through ISlide.Comments property.
  • Iterate over each comment to get its text content through Comment.Text property, and then append to the StringBuilder instance.
  • Write to a text file using Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;
using System.IO;
using System.Text;

namespace ExtractComment
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation instance
            Presentation presentation = new Presentation();

            // Load a PowerPoint presentation
            presentation.LoadFromFile("Comments.pptx");

            // Create a StringBuilder instance
            StringBuilder str = new StringBuilder();

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Get all comments in the slide
            Comment[] comments = slide.Comments;

            // Append the comment text to the StringBuilder instance
            for (int i = 0; i < comments.Length; i++)
            {
                str.Append(comments[i].Text + "\r\n");
            }

            // Write to a text file
            File.WriteAllText("ExtractComment.txt", str.ToString());
        }
    }
}

Get all the comments on the first slide and export to a text file

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.