codecamp

Apache POI PPT - 创建超链接

在本章中,您将学习如何在演示文稿中创建超链接。

创建超链接

您可以使用 XSLFTextRun 类的 createHyperlink()方法读取演示文稿中的超链接。 按照下面给出的过程在演示中创建超链接。

使用 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");

使用 XSLFTextRun 类的 createHyperlink()方法创建超链接,如下所示:

XSLFHyperlink link = textRun.createHyperlink();

使用 XSLFHyperlink 类的 setAddress()方法将链接地址设置为超链接,如下所示:

link.setAddress("//www.w3cschool.cn/");

下面给出了在演示文稿中创建超链接的完整程序:

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()[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("//www.w3cschool.cn/");
      
      //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 cretated successfully");
      out.close();              
   }
}

将上述Java代码保存为 CreatingHyperlinks.java ,然后从命令提示符处编译并执行,如下所示:

$javac CreatingHyperlinks.java
$java CreatingHyperlinks

它将编译并执行以生成以下输出:

slide cretated successfully 

新添加的幻灯片及其正文中的超链接如下所示:


Apache POI PPT - 图片
Apache POI PPT - 读取形状
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }