Apache Poi Ppt 简明教程
Apache POI PPT - Creating Hyperlinks
在本章中,你将学习如何在演示文稿中创建超链接。
In this chapter you will learn how to create hyperlinks in a presentation.
Creating Hyperlinks
你可以使用 XSLFTextRun 类的 createHyperlink() 方法来读取演示文稿中的超链接。按照下面给出的步骤,在演示文稿中创建一个超链接。
You can read the hyperlinks in a presentation using the createHyperlink() method of the XSLFTextRun class. Follow the procedure given below to create a hyperlink in a presentation.
使用 XMLSlideShow 类创建一个空演示文稿,如下所示:
Create an empty presentation using the XMLSlideShow class as shown below −
XMLSlideShow ppt = new XMLSlideShow();
创建一个空幻灯片,并使用主体和内容布局创建一个文本框和幻灯片的正文。
Create an empty slide and create a textbox and body of the slide using body and content layout.
//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();
创建文本运行对象,并设置其文本,如下所示:
Create a text run object and set text to it as shown below −
XSLFTextRun textRun = body.addNewTextParagraph().addNewTextRun();
textRun.setText("Tutorials point");
使用 XSLFTextRun 类的 createHyperlink() 方法创建一个超链接,如下所示:
Create a hyperlink using the createHyperlink() method of the XSLFTextRun class as shown below −
XSLFHyperlink link = textRun.createHyperlink();
使用 XSLFHyperlink 类的 setAddress() 方法设置超链接的链接地址,如下所示:
Set the link address to the hyperlink using the setAddress() method of XSLFHyperlink class as shown below −
link.setAddress("https://www.tutorialspoint.com/");
下面给出了在演示文稿中创建超链接的完整程序:
Given below is the complete program to create hyperlink in a presentation −
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://www.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 ,然后从命令提示符编译并执行,如下所示:
Save the above Java code as CreatingHyperlinks.java, and then compile and execute it from the command prompt as follows −
$javac CreatingHyperlinks.java
$java CreatingHyperlinks
它将编译和执行以生成以下输出 −
It will compile and execute to generate the following output −
slide created successfully
新添加的幻灯片及其正文中的超链接如下所示:
The newly added slide with the hyperlink in its body looks as follows −