poi操作ppt添加超链接

本文介绍如何使用Apache POI 3.14版本创建包含超链接的PowerPoint演示文稿。具体包括:创建指向网址的超链接及跳转到同一演示文稿中另一页面的超链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值