概述
幻燈片中的備註信息是隻提供給幻燈片演講者觀看的特定內容,在演講者放映幻燈片時,備註信息可給演講者提供講解思路,起到輔助講解的作用。本文將通過Java程序來演示如何操作PPT幻燈片中的備註信息,要點包括:
- 添加備註信息
- 修改備註信息
- 讀取備註信息
- 刪除備註信息
使用工具
- Free Spire.Presentation for Java (免費版)
Jar文件獲取及導入:
方法1:通過官網下載JAR文件包。下載後,解壓文件,並將lib文件夾下的Spire.Presentation.jar文件導入到java程序。參考如下導入效果:
方法2:可通過maven倉庫安裝導入到maven項目,可參考導入方法。
Java 代碼示例
【示例1】添加備註信息
import com.spire.presentation.*;
public class AddSpeakNotes {
public static void main(String[] args) throws Exception{
//加載PowerPoint文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.getSlides().get(2);
//添加備註幻燈片到第一張幻燈片
NotesSlide notesSlide = slide.addNotesSlide();
//添加備註標題
ParagraphEx paragraph = new ParagraphEx();
String string = "備註:";
paragraph.setText(string);
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("AddSpeakerNotes.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
備註添加效果:
【示例2】修改備註信息
import com.spire.presentation.*;
public class ModifySpeakerNotes {
public static void main(String[] args) throws Exception{
//加載測試文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("AddSpeakerNotes.pptx
");
//獲取指定幻燈片
ISlide slide = ppt.getSlides().get(2);
//修改指定備註信息
slide.getNotesSlide().getNotesTextFrame().getParagraphs().get(1).setText("新修改的備註信息");
slide.getNotesSlide().getNotesTextFrame().getParagraphs().get(2).setAlignment(TextAlignmentType.CENTER);
slide.getNotesSlide().getNotesTextFrame().getParagraphs().get(2).setBulletStyle(NumberedBulletStyle.BULLET_ROMAN_UC_PAREN_RIGHT);
//保存文檔
ppt.saveToFile("modifySpeakerNotes.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
備註修改效果:
【示例3】讀取備註信息
import com.spire.presentation.*;
import java.io.FileWriter;
public class ExtractSpeakerNotes {
public static void main(String[] args) throws Exception{
//加載測試文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("AddSpeakerNotes.pptx");
//獲取指定幻燈片
ISlide slide = ppt.getSlides().get(2);
//獲取幻燈片中的備註內容
StringBuilder builder = new StringBuilder();
String notes = slide.getNotesSlide().getNotesTextFrame().getText();
builder.append(notes);
//保存到文本文檔
FileWriter writer = new FileWriter("ExtractSpeakerNotes.txt");
writer.write(builder.toString());
writer.flush();
writer.close();
}
}
備註信息讀取結果:
【示例4】刪除備註信息
import com.spire.presentation.*;
public class DeleteSpeakerNotes {
public static void main(String[] args) throws Exception{
//加載測試文檔
Presentation ppt = new Presentation();
ppt.loadFromFile("test.pptx");
//獲取指定幻燈片
ISlide slide = ppt.getSlides().get(2);
//刪除備註信息
slide.getNotesSlide().getNotesTextFrame().getParagraphs().get(0).getTextRanges().clear();//刪除指定段落中的備註信息
//slide.getNotesSlide().getNotesTextFrame().getParagraphs().clear();//刪除所有備註信息
//保存文檔
ppt.saveToFile("deleteSpeakerNotes.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
備註信息刪除效果:
(本文完)
轉載請註明出處!