- 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 - 建立超連結
本章將學習如何在簡報中建立超連結。
建立超連結
可以使用 createHyperlink() 方法從 XSLFTextRun 類讀取簡報中的超連結。按照以下所示步驟在簡報中建立超連結。
如以下所示,使用 XMLSlideShow 類建立空簡報。−
XMLSlideShow ppt = new XMLSlideShow();
建立空幻燈片並在正文和內容佈局中建立文字框和幻燈片正文。
//create an empty presentation XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0]; //creating a slide with title and content layout XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT); XSLFSlide slide = ppt.createSlide(slidelayout); //selection of body place holder XSLFTextShape body = slide.getPlaceholder(1); //clear the existing text in the slide body.clearText();
建立文字執行物件並向其設定文字,如以下所示 −
XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
textRun.setText("Tutorials point");
如以下所示,使用 createHyperlink() 方法從 XSLFTextRun 類建立超連結 −
XSLFHyperlink link = textRun.createHyperlink();
如以下所示,使用 XSLFHyperlink 類的 setAddress() 方法設定超連結的連結地址 −
link.setAddress("https://tutorialspoint.tw/");
以下是建立簡報超連結的完整程式 −
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.XSLFHyperlink;
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.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
public class CreatingHyperlinks {
public static void main(String args[]) throws IOException {
//create 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 slid
body.clearText();
//adding new paragraph
XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
//setting the text
textRun.setText("Tutorials point");
//creating the hyperlink
XSLFHyperlink link = textRun.createHyperlink();
//setting the link address
link.setAddress("https://tutorialspoint.tw/");
//create the file object
File file = new File("hyperlink.pptx");
FileOutputStream out = new FileOutputStream(file);
//save the changes in a file
ppt.write(out);
System.out.println("slide created successfully");
out.close();
}
}
將以上 Java 程式碼儲存為 CreatingHyperlinks.java,然後按如下所示從命令提示符處進行編譯和執行: −
$javac CreatingHyperlinks.java $java CreatingHyperlinks
編譯並執行可生成以下輸出 −
slide created successfully
在正文中包含超連結的新增幻燈片如下所示 −
廣告