Working with Comments and Speaker Notes — Aspose.Slides FOSS for Java
Aspose.Slides FOSS for Java supports two types of annotations: threaded slide comments (visible in review mode) and speaker notes (visible in Presenter View and the Notes pane).
Threaded Comments
Comments are attached to a slide and associated with an author. The prs.getCommentAuthors() collection manages all authors; each author has a getComments() collection for adding and reading comments.
Add a Comment
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.PointF;
import org.aspose.slides.foss.SaveFormat;
import java.time.LocalDateTime;
try (Presentation prs = new Presentation()) {
// Create a comment author with initials
var author = prs.getCommentAuthors().addAuthor("Jane Smith", "JS");
var slide = prs.getSlides().get(0);
// Add a comment at position (2.0, 2.0) inches from the slide top-left
author.getComments().addComment(
"Please review the figures on this slide",
slide,
new PointF(2.0f, 2.0f),
LocalDateTime.now()
);
prs.save("commented.pptx", SaveFormat.PPTX);
}The PointF position is in inches from the top-left corner of the slide. Multiple comments can be added to the same slide by calling addComment() again.
Multiple Authors and Comments
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.PointF;
import org.aspose.slides.foss.SaveFormat;
import java.time.LocalDateTime;
try (Presentation prs = new Presentation()) {
var author1 = prs.getCommentAuthors().addAuthor("Alice Brown", "AB");
var author2 = prs.getCommentAuthors().addAuthor("Bob Davis", "BD");
var slide = prs.getSlides().get(0);
author1.getComments().addComment("Initial draft", slide, new PointF(1.0f, 1.0f), LocalDateTime.now());
author2.getComments().addComment("Approved", slide, new PointF(3.0f, 1.0f), LocalDateTime.now());
prs.save("multi-author.pptx", SaveFormat.PPTX);
}Read Comments from an Existing File
import org.aspose.slides.foss.Presentation;
try (Presentation prs = new Presentation("commented.pptx")) {
for (int a = 0; a < prs.getCommentAuthors().size(); a++) {
var author = prs.getCommentAuthors().get(a);
System.out.println("Author: " + author.getName() + " (" + author.getInitials() + ")");
for (int c = 0; c < author.getComments().size(); c++) {
var comment = author.getComments().get(c);
System.out.println(" " + comment.getText());
}
}
}Speaker Notes
Speaker notes are stored on a per-slide basis via a NotesSlide object. Access it through slide.getNotesSlideManager().
Add Speaker Notes to a Slide
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
try (Presentation prs = new Presentation()) {
var slide = prs.getSlides().get(0);
slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 50, 50, 400, 200);
// Create the notes slide and set text
var notes = slide.getNotesSlideManager().addNotesSlide();
notes.getNotesTextFrame().setText(
"Mention the Q3 revenue increase on this slide. Emphasize the 24% growth."
);
prs.save("with-notes.pptx", SaveFormat.PPTX);
}Simpler Notes Example
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
try (Presentation prs = new Presentation()) {
var slide = prs.getSlides().get(0);
slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 100, 100, 500, 250)
.addTextFrame("Main Content");
var notes = slide.getNotesSlideManager().addNotesSlide();
notes.getNotesTextFrame().setText("These are the speaker notes for this slide.");
prs.save("notes.pptx", SaveFormat.PPTX);
}Check Whether a Notes Slide Already Exists
getNotesSlideManager().getNotesSlide() returns null if no notes slide has been created yet:
import org.aspose.slides.foss.Presentation;
try (Presentation prs = new Presentation("existing.pptx")) {
for (int i = 0; i < prs.getSlides().size(); i++) {
var slide = prs.getSlides().get(i);
var existingNotes = slide.getNotesSlideManager().getNotesSlide();
if (existingNotes != null) {
String text = existingNotes.getNotesTextFrame().getText();
System.out.println("Slide " + (i + 1) + " notes: " + text);
} else {
System.out.println("Slide " + (i + 1) + ": no notes");
}
}
}Add Notes to Multiple Slides
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
String[] noteTexts = {
"Opening remarks: introduce the agenda.",
"Key metrics: emphasize Q4 results.",
"Closing: call to action.",
};
try (Presentation prs = new Presentation()) {
// Add slides 2 and 3
var layout = prs.getSlides().get(0).getLayoutSlide();
prs.getSlides().addEmptySlide(layout);
prs.getSlides().addEmptySlide(layout);
for (int i = 0; i < prs.getSlides().size(); i++) {
var slide = prs.getSlides().get(i);
slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 50, 50, 600, 300)
.addTextFrame("Slide " + (i + 1));
var n = slide.getNotesSlideManager().addNotesSlide();
n.getNotesTextFrame().setText(noteTexts[i]);
}
prs.save("all-notes.pptx", SaveFormat.PPTX);
}