Apache POI PPT 创建超链接

为了在 PPT 中设置超链接,Apache POI 提供了 createHyperlink() 方法,负责在幻灯片上创建超链接。看下面的例子,我们用Java程序演示了PPT 中的超链接实现。

package com.yiidian;

import org.apache.poi.xslf.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class ReadImageExample {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        XMLSlideShow ppt = new XMLSlideShow();
        try (OutputStream os = new FileOutputStream("yiidian.pptx")) {
            // Setting layout  
            XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
            XSLFSlideLayout tc = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
            XSLFSlide slide = ppt.createSlide(tc);
            // Setting title  
            XSLFTextShape title = slide.getPlaceholder(0);  
            title.setText("Hyperlink Example");  
            // Setting body  
            XSLFTextShape body = slide.getPlaceholder(1);  
            body.clearText();  
            XSLFTextRun r = body.addNewTextParagraph().addNewTextRun();  
            r.setText("点击这里访问一点教程网.");
            XSLFHyperlink link = r.createHyperlink();  
            link.setAddress("http://www.yiidian.com");
            ppt.write(os);  
        }catch(Exception e) {  
             System.out.println(e);  
         }  
    }  
}  

输出结果为:

热门文章

优秀文章