- 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.com/");
以下是创建演示文稿超链接的完整程序 −
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.com/"); //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
在正文中包含超链接的新增幻灯片如下所示 −
广告