Thursday, 18 February 2021

[Java] Add, Read and Delete Speaker Notes in a PowerPoint document

Speaker Notes in PowerPoint slides are a short paragraph that reminds the speaker of the contents of the current slide in the slide-show, and they are an important tool in ensuring a smooth presentation for your work. The most interesting thing about them is the fact that they are not viewable by the audience during a presentation. This article will show you how to add, read and delete speaker notes using Java codes.

Using the tool

In this tutorial, I used a free third-party library called FreeSpire.Presentation for Java. It is a professional PowerPoint API that enables developers to create, edit, read, convert and save PowerPoint documents in Java Applications without installing Microsoft PowerPoint.

Before typing codes, you need to add the Jar file in the library to your project. You can download it from the link, and manually add it to IDEA, or directly reference it using the following Maven configuration.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Using the code

Add Speaker Notes

import com.spire.presentation.*;
public class AddSpeakerNotes {
public static void main(String[] args) throws Exception {
//Load the PowerPoint document
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Get the first slide
ISlide slide = ppt.getSlides().get(0);
//Add note slide
NotesSlide notesSlide = slide.addNotesSlide();

//Add a paragraph to the note slide
ParagraphEx paragraph = new ParagraphEx();
paragraph.setText("Tips for making effective presentations:");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
//Set the bullet type and style for the paragraph
notesSlide.getNotesTextFrame().getParagraphs().get(0).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(0).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);

//Add a paragraph to the note slide
paragraph = new ParagraphEx();
paragraph.setText("Use the slide master feature to create a consistent and simple design template.");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
//Set the bullet type and style for the paragraph
notesSlide.getNotesTextFrame().getParagraphs().get(2).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(2).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);

//Add a paragraph to the note slide
paragraph = new ParagraphEx();
paragraph.setText("Simplify and limit the number of words on each screen.");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
//Set the bullet type and style for the paragraph
notesSlide.getNotesTextFrame().getParagraphs().get(3).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(3).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);

//Add a paragraph to the note slide
paragraph = new ParagraphEx();
paragraph.setText("Use contrasting colors for text and background.");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
//Set the bullet and style type for the paragraph
notesSlide.getNotesTextFrame().getParagraphs().get(4).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(4).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);

//Save the document
ppt.saveToFile("output/AddSpeakerNotes.pptx", FileFormat.PPTX_2013);
}
}

Output


Read Speaker Notes

import com.spire.presentation.*;
import java.io.FileWriter;

public class ReadSpeakerNotes {
public static void main(String[] args) throws Exception {
//Load the PowerPoint document
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\AddSpeakerNotes.pptx");

//Get the first slide
ISlide slide = ppt.getSlides().get(0);

//Get the content of notes from note slide
StringBuilder buffer = new StringBuilder();
String notes = slide.getNotesSlide().getNotesTextFrame().getText();
buffer.append(notes);

//Save to .txt file
FileWriter writer = new FileWriter("output/ReadSpeakerNotes.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
}
}

Output



Delete Speaker Notes

import com.spire.presentation.*;

public class DeleteSpeakerNotes {
public static void main(String[] args) throws Exception {
//Load the PowerPoint document
Presentation ppt = new Presentation();
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\AddSpeakerNotes.pptx");

//Get the first slide
ISlide slide = ppt.getSlides().get(0);

//Delete all speaker notes from the note slide
slide.getNotesSlide().getNotesTextFrame().getParagraphs().clear();

//Save the document
ppt.saveToFile("output/DeleteSpeakerNotes.pptx", FileFormat.PPTX_2013);
}
}

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...