Add, Delete or Replace PowerPoint Comments in React with JavaScript

A presentation usually goes through several rounds of review, and the comments left on the slides ("add a data chart here", "make the title shorter") are the record of that review. Before the final version ships, those notes have to be archived, or the outdated ones cleared out. Doing it by hand one comment at a time is slow, and on a shared file it is easy to miss one. Running it as a server-side batch means the file has to leave the user's device first.

This article uses Spire.Presentation for JavaScript to add, replace and delete PowerPoint comments directly in the browser. The whole process runs in the browser via WebAssembly, with no Office installation and no backend dependency, and the file is read and written locally through the virtual file system (VFS).

This article covers three core tasks:

For installation and project setup, see Integrate Spire.Presentation for JavaScript in a React project. The examples below assume Spire.Presentation is installed and the WebAssembly module is initialized.


Add PowerPoint comments

Add a comment with Slide.AddComment():

Call What it adds
AddComment({ author, text, position, dateTime }) A new comment with the given author, content, position and time
AddComment({ comment }) An existing comment object added to the current slide

position is a PointF, with coordinates in points measured from the top-left corner of the slide.

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready.
  2. Load the sample presentation and fonts, create a Presentation object and load the file.
  3. Register an author with Presentation.CommentAuthors.AddAuthor(), then add a comment with Presentation.Slides.get_Item().AddComment().
  4. Save the result with Presentation.SaveToFile().
  5. Read the generated file from the VFS and trigger a browser download.

The code to add a PowerPoint comment is as follows:

function App() {
  const addComment = async () => {
    // Get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // Check that the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // Load the sample presentation and fonts
    await window.spire.FetchFileToVFS('SamplePresentation.pptx', "", `${process.env.PUBLIC_URL}/data/`);
    await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);

    // Create a Presentation instance and load the presentation
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile('SamplePresentation.pptx');

    // Register the comment author
    const author = ppt.CommentAuthors.AddAuthor('Eric', 'ER');

    // Add a comment to the first slide at (60, 80) points near the top-left corner
    ppt.Slides.get_Item(0).AddComment({
      author: author,
      text: 'Please add a one-line product intro.',
      position: new pptModule.PointF(60, 80),
      dateTime: new Date(),
    });

    // Save the result file
    const outputFileName = 'Added-Comment.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // Read the generated file from the VFS and trigger a download
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Add PowerPoint Comments</h1>
      <button onClick={addComment}>
        Add Comment
      </button>
    </div>
  );
}

export default App;

A comment by Eric has been added to the first slide:

A comment by Eric has been added to the first slide


Replace PowerPoint comments

Replacing means rewriting a comment's Text; the sample loops over Slide.Comments, finds Nina's comment and changes it.

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready.
  2. Load the sample presentation and fonts, create a Presentation object and load the file.
  3. Get the comment collection with Presentation.Slides.get_Item().Comments, loop to find the target comment and write its Text property.
  4. Save the result with Presentation.SaveToFile().
  5. Read the generated file from the VFS and trigger a browser download.

The code to replace a PowerPoint comment is as follows:

function App() {
  const replaceComment = async () => {
    // Get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // Check that the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // Load the sample presentation and fonts
    await window.spire.FetchFileToVFS('SamplePresentation.pptx', "", `${process.env.PUBLIC_URL}/data/`);
    await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);

    // Create a Presentation instance and load the presentation
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile('SamplePresentation.pptx');

    // Get the comment collection of the first slide
    const comments = ppt.Slides.get_Item(0).Comments;

    // Find the target comment by author and replace its text
    for (let i = 0; i < comments.length; i++) {
      if (comments[i].AuthorName === 'Nina') {
        comments[i].Text = 'Title updated, please review.';
        break;
      }
    }

    // Save the result file
    const outputFileName = 'Replaced-Comment.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // Read the generated file from the VFS and trigger a download
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Replace PowerPoint Comments</h1>
      <button onClick={replaceComment}>
        Replace Comment
      </button>
    </div>
  );
}

export default App;

Nina's comment now reads "Title updated, please review.":

Nina's comment now reads


Delete PowerPoint comments

Delete with Slide.DeleteComment(), four overloads from narrowest to widest scope:

Call Comments deleted
DeleteComment({ comment }) That one specific comment object
DeleteComment({ author, text }) Comments matching both the author and the text
DeleteComment({ author }) All comments by that author on this slide
DeleteComment({ text }) All comments whose text equals the given content

The sample deletes Cral's comment with the { comment } overload.

The steps are:

  1. Get the Spire.Presentation WASM module and make sure it is ready.
  2. Load the sample presentation and fonts, create a Presentation object and load the file.
  3. Loop over Presentation.Slides.get_Item().Comments to find the target comment, then delete it with Slide.DeleteComment().
  4. Save the result with Presentation.SaveToFile().
  5. Read the generated file from the VFS and trigger a browser download.

The code to delete a PowerPoint comment is as follows:

function App() {
  const deleteComment = async () => {
    // Get the Spire.Presentation WASM module
    const pptModule = window.wasmModule?.spirepresentation;

    // Check that the module is ready
    if (!pptModule) {
      alert('Spire.Presentation is not ready yet');
      return;
    }

    // Load the sample presentation and fonts
    await window.spire.FetchFileToVFS('SamplePresentation.pptx', "", `${process.env.PUBLIC_URL}/data/`);
    await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);

    // Create a Presentation instance and load the presentation
    const ppt = new pptModule.Presentation();
    ppt.LoadFromFile('SamplePresentation.pptx');

    // Get the comment collection of the first slide
    const slide = ppt.Slides.get_Item(0);
    const comments = slide.Comments;

    // Find the target comment by author and delete it by object
    for (let i = 0; i < comments.length; i++) {
      if (comments[i].AuthorName === 'Cral') {
        slide.DeleteComment({ comment: comments[i] });
        break;
      }
    }

    // Save the result file
    const outputFileName = 'Deleted-Comment.pptx';
    ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
    ppt.Dispose();

    // Read the generated file from the VFS and trigger a download
    const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
    const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = outputFileName;
    a.click();
    URL.revokeObjectURL(url);
  };

  return (
    <div style={{ textAlign: 'center', height: '300px' }}>
      <h1>Delete PowerPoint Comments</h1>
      <button onClick={deleteComment}>
        Delete Comment
      </button>
    </div>
  );
}

export default App;

Cral's comment has been removed from the first slide, while Nina's comment is kept:

Cral's comment has been removed from the first slide, while Nina's comment is kept

Delete a comment by author and text

When you do not want to loop over Comments first, pass the author and text directly. Get the author object with CommentAuthors.FindByName(), then hand it to DeleteComment({ author, text }); it deletes only when both conditions hold:

// Get the author object and delete by author and text
const author = ppt.CommentAuthors.FindByName('Cral')[0];
ppt.Slides.get_Item(0).DeleteComment({ author: author, text: 'Please add a data chart.' });

FAQ

Why register an author before adding a comment

Cause: The author of AddComment({ author, ... }) must be an ICommentAuthor object; the library passes its handle to the underlying engine, and a plain author name string is not recognized.

Solution: Register the author first with Presentation.CommentAuthors.AddAuthor(name, initials) and pass the returned object when adding the comment. Authors already present in the file do not need to be registered again; get them with CommentAuthors.FindByName(name).

What unit does a comment's position use

Cause: position is a PointF, with coordinates in points measured right and down from the top-left corner of the slide.

Solution: Pick the landing point according to the slide size; a default 16:9 page is 960 × 540 points, so (60, 80) in the sample sits near the top-left corner. To pin a comment to an element, use that element's Left and Top as a reference.

How many comments are deleted when deleting by author or text

Cause: Passing only an author or only text is a broad condition, and every matching comment on the slide is deleted. With { author, text } both conditions must hold at once, so the hit set is much smaller.

Solution: If you are unsure of the scope, loop over Slide.Comments first and print AuthorName and Text to check, then pick the right overload. To delete exactly one comment, passing the concrete object with { comment } is the safest.

Can a comment's reply be deleted on its own

Cause: A reply appended by Comment.Reply() is also stored as a comment on the same slide, can be retrieved from Comments, has IsReply set to true, and is matched by author and text when deleting.

Solution: Use DeleteComment({ author, text }) with the reply's author and content to delete that reply; the original comment is unaffected.

Can comments on multiple slides be replaced or deleted at once

Cause: AddComment and DeleteComment hang off Slide and act on only the current slide at a time.

Solution: Loop over Presentation.Slides and process each slide's Comments the same way. When deleting in bulk, gather all the matches first and then delete, to avoid modifying the collection while iterating it.


Get a Free License

If you want to remove the evaluation message from the resulting document or lift the feature limits, contact sales for a temporary license valid for 30 days.