这篇文章将介绍如何使用Spire.Presentation for Java添加、读取和删除PowerPoint幻灯片中的备注信息。
添加备注信息
import com.spire.presentation.*;
import java.io.FileWriter;
public class SpeakerNotes {
public static void main(String[] args) throws Exception {
//加载PowerPoint文档
Presentation ppt = new Presentation();
ppt.loadFromFile("Sample.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//添加备注幻灯片到第一张幻灯片
NotesSlide notesSlide = slide.addNotesSlide();
//添加备注标题
ParagraphEx paragraph = new ParagraphEx();
paragraph.setText("备注:");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
//添加第一项备注
paragraph = new ParagraphEx();
paragraph.setText("第一项备注;");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
notesSlide.getNotesTextFrame().getParagraphs().get(1).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(1).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
//添加第二项备注
paragraph = new ParagraphEx();
paragraph.setText("第二项备注;");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
notesSlide.getNotesTextFrame().getParagraphs().get(2).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(2).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
//添加第三项备注
paragraph = new ParagraphEx();
paragraph.setText("第三项备注;");
notesSlide.getNotesTextFrame().getParagraphs().append(paragraph);
notesSlide.getNotesTextFrame().getParagraphs().get(3).setBulletType(TextBulletType.NUMBERED);
notesSlide.getNotesTextFrame().getParagraphs().get(3).setBulletStyle(NumberedBulletStyle.BULLET_ARABIC_PERIOD);
//保存文档
ppt.saveToFile("SpeakerNotes.pptx", FileFormat.PPTX_2013);
}
}
读取备注信息
import com.spire.presentation.*;
import java.io.FileWriter;
public class SpeakerNotes {
public static void main(String[] args) throws Exception {
//加载PowerPoint文档
Presentation ppt = new Presentation();
ppt.loadFromFile("SpeakerNotes.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//获取幻灯片中的备注内容
StringBuilder buffer = new StringBuilder();
String notes = slide.getNotesSlide().getNotesTextFrame().getText();
buffer.append(notes);
//保存到文本文档
FileWriter writer = new FileWriter("SpeakerNotes.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
}
}
删除备注信息
import com.spire.presentation.*;
import java.io.FileWriter;
public class SpeakerNotes {
public static void main(String[] args) throws Exception {
//加载PowerPoint文档
Presentation ppt = new Presentation();
ppt.loadFromFile("SpeakerNotes.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//删除幻灯片中所有备注
slide.getNotesSlide().getNotesTextFrame().getParagraphs().clear();
//保存文档
ppt.saveToFile("DeleteSpeakerNotes.pptx", FileFormat.PPTX_2013);
}
}