poi操作ppt添加超链接

来源:互联网 发布:知图科技ceo 编辑:程序博客网 时间:2024/05/01 15:50

POI版本3.14。

创建超链接跳转到一个url,添加一个超链接跳转到另一个ppt页面。

/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */package org.apache.poi.hslf.examples;import java.awt.Rectangle;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hslf.usermodel.HSLFHyperlink;import org.apache.poi.hslf.usermodel.HSLFSlide;import org.apache.poi.hslf.usermodel.HSLFSlideShow;import org.apache.poi.hslf.usermodel.HSLFTextBox;/** * Demonstrates how to create hyperlinks in PowerPoint presentations */public abstract class CreateHyperlink {        public static void main(String[] args) throws IOException {        //创建ppt对象        HSLFSlideShow ppt = new HSLFSlideShow();        //构造页面        HSLFSlide slideA = ppt.createSlide();        //加第二个页面        HSLFSlide slideB = ppt.createSlide();        //第三页ppt        HSLFSlide slideC = ppt.createSlide();        //第一个页面加一个文本框        HSLFTextBox textBox1 = slideA.createTextBox();        //文本框设置文字        textBox1.setText("Apache POI");        //设置位置和大小        textBox1.setAnchor(new Rectangle(100, 100, 200, 50));        //在文本框的文字上添加超链接        HSLFHyperlink link1 = textBox1.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();        //设置连接URL        link1.linkToUrl("http://www.apache.org");        //设置鼠标悬浮的文字        link1.setLabel(textBox1.getText());        //又添加一个文本框        HSLFTextBox textBox2 = slideA.createTextBox();        textBox2.setText("Go to slide #3");        textBox2.setAnchor(new Rectangle(100, 300, 200, 50));        //添加超链        HSLFHyperlink link2 = textBox2.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();        //超链到第三个ppt页面        link2.linkToSlide(slideC);        HSLFTextBox textBox3 = slideC.createTextBox();        textBox3.setText("third page");        textBox3.setAnchor(new Rectangle(200, 200, 200, 200));        FileOutputStream out = new FileOutputStream("hyperlink.ppt");        ppt.write(out);        out.close();        ppt.close();   }}


0 0