Java添加、读取和删除 PPT 中的备注
程序员文章站
2022-03-25 15:24:14
...
PowerPoint 幻灯片中的备注信息是为使用者提供便利而设置的,该信息只对使用者可见。本文将向大家详细阐述如何通过Spire.Presentation for Java添加、读取和删除 PowerPoint 幻灯片中的备注信息。
使用工具:Free Spire.Presentation for Java 2.2.3(免费版)
注:编辑代码前,注意添加引用lib文件夹中的jar文件到程序(如下图)
示例1—添加备注到PPT:
import com.spire.presentation.*; public class AddNotes { public static void main(String[] args) throws Exception { //加载PowerPoint文档 Presentation ppt = new Presentation(); ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\222.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); } }
备注添加效果:
示例2--读取PPT备注:
import com.spire.presentation.ISlide; import com.spire.presentation.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(); } }
读取结果:
示例3--删除备注
import com.spire.presentation.FileFormat; import com.spire.presentation.ISlide; import com.spire.presentation.Presentation; public class DeleteNotes { 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); } }
删除结果:
(本文完)