- Apache POI PPT 教程
- Apache POI PPT - 主頁
- Apache POI PPT - 概覽
- Apache POI PPT - Java API 版本
- Apache POI PPT - 安裝
- Apache POI PPT - 類和方法
- Apache POI PPT - 簡報
- Apache POI PPT - 幻燈片版式
- Apache POI PPT - 管理幻燈片
- Apache POI PPT - 影像
- Apache POI PPT - 建立超連結
- Apache POI PPT - 讀取形狀
- Apache POI PPT - 格式化文字
- Apache POI PPT - 合併
- Apache POI PPT - PPT 轉影像
- Apache POI PPT 資源
- Apache POI PPT - 快速指南
- Apache POI PPT - 有用資源
- Apache POI PPT - 討論
Apache POI PPT - 格式化文字
可以使用 XSLFTextRun 類的各種方法來格式化簡報中的文字。為此,必須透過如下所示選擇一個幻燈片版式來建立一個 XSLFTextRun 類物件 −
//create the empty presentation XMLSlideShow ppt = new XMLSlideShow(); //getting the slide master object XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(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(); //creating text run object XSLFTextRun run = paragraph.addNewTextRun();
可以使用 setFontSize() 設定簡報中文字的字型大小。
run.setFontColor(java.awt.Color.red); run.setFontSize(24);
下面的程式碼片段說明了如何將不同的格式化樣式(加粗、斜體、下劃線、刪除線)應用到簡報中的文字。
//change the text into bold format run.setBold(true); //change the text it to italic format run.setItalic(true) // strike through the text run.setStrikethrough(true); //underline the text run.setUnderlined(true);
如需在段落之間插入換行符,請使用 XSLFTextParagraph 類中的 addLineBreak(),如下所示 −
paragraph.addLineBreak();
下面給出了使用上述所有方法格式化文字的完整程式 −
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 TextFormating {
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().get(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.0);
//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.0);
//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.setUnderlined(true);
//underlining the text
paragraph.addLineBreak();
//creating a file object
File file = new File(“TextFormat.pptx”);
FileOutputStream out = new FileOutputStream(file);
//saving the changes to a file
ppt.write(out);
out.close();
}
}
將上述程式碼儲存為 TextFormating.java,然後在命令提示符處進行編譯和執行,如下所示 −
$javac TextFormating.java $java TextFormating
程式碼會編譯並執行,生成以下輸出 −
Formatting completed successfully
格式化文字後的幻燈片如下所示 −
廣告