- Java 编程示例
- 示例 - 首页
- 示例 - 环境
- 示例 - 字符串
- 示例 - 数组
- 示例 - 日期和时间
- 示例 - 方法
- 示例 - 文件
- 示例 - 目录
- 示例 - 异常
- 示例 - 数据结构
- 示例 - 集合
- 示例 - 网络
- 示例 - 线程
- 示例 - 小程序
- 示例 - 简单 GUI
- 示例 - JDBC
- 示例 - 正则表达式
- 示例 - Apache PDF Box
- 示例 - Apache POI PPT
- 示例 - Apache POI Excel
- 示例 - Apache POI Word
- 示例 - OpenCV
- 示例 - Apache Tika
- 示例 - iText
- Java 教程
- Java - 教程
- Java 有用资源
- Java - 快速指南
- Java - 有用资源
如何使用 Java 格式化 PPT 中幻灯片上的文本
问题描述
如何使用 Java 格式化 PPT 中幻灯片上的文本。
解决方案
以下是使用 Java 格式化 PPT 中幻灯片上文本的程序。
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.SlideLayout; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFSlideLayout; import org.apache.poi.xslf.usermodel.XSLFSlideMaster; import org.apache.poi.xslf.usermodel.XSLFTextParagraph; import org.apache.poi.xslf.usermodel.XSLFTextRun; import org.apache.poi.xslf.usermodel.XSLFTextShape; public class FormatTextPPT { public static void main(String args[]) throws IOException { //creating an empty presentation XMLSlideShow ppt = new XMLSlideShow(); //getting the slide master object XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0]; //select a layout from specified list XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT); //creating a slide with title and content layout XSLFSlide slide = ppt.createSlide(slidelayout); //selection of title place holder XSLFTextShape body = slide.getPlaceholder(1); //clear the existing text in the slide body.clearText(); //adding new paragraph XSLFTextParagraph paragraph = body.addNewTextParagraph(); //formatting line 1 XSLFTextRun run1 = paragraph.addNewTextRun(); run1.setText("This is a colored line"); //setting color to the text run1.setFontColor(java.awt.Color.red); //setting font size to the text run1.setFontSize(24); //moving to the next line paragraph.addLineBreak(); //formatting line 2 XSLFTextRun run2 = paragraph.addNewTextRun(); run2.setText("This is a bold line"); run2.setFontColor(java.awt.Color.CYAN); //making the text bold run2.setBold(true); paragraph.addLineBreak(); //formatting line 3 XSLFTextRun run3 = paragraph.addNewTextRun(); run3.setText(" This is a striked line"); run3.setFontSize(12); //making the text italic run3.setItalic(true); //strike through the text run3.setStrikethrough(true); paragraph.addLineBreak(); //formatting line 4 XSLFTextRun run4 = paragraph.addNewTextRun(); run4.setText(" This an underlined line"); run4.setUnderline(true); //underlining the text paragraph.addLineBreak(); //creating a file object File file = new File("C:/poippt/FormatedText.pptx"); FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); out.close(); System.out.println("PPT created"); } }
结果
java_apache_poi_ppt
广告