Java 在PPT中添加文本和图片超链接的实现方法

所属分类: 软件编程 / java 阅读数: 50
收藏 0 赞 0 分享

在文档中添加超链接,可以快速从当前文档跳转至指定的网页或打开指定的外部文件。前文中我们介绍过如何使用Java程序来为Word文档和Excel工作表添加超链接。本文将演示如何在PPT中添加文本和图片超链接。

使用工具:Free Spire.Presentation for Java(免费版)Jar文件获取及导入:

方法1:通过官网下载获取jar包。解压后将lib文件夹下的Spire.Presentation.jar文件导入Java程序。(如下图)

方法2:通过maven仓库安装导入。具体安装教程参见此网页

【示例1】添加文本超链接

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TextHyperlink {
  public static void main(String[] args) throws Exception {
    //创建一个PPT文档,默认包含一张幻灯片
    Presentation presentation = new Presentation();

    //在文档最后追加一张幻灯片并填充一些内容,方便之后添加超链接链接到此幻灯片
    presentation.getSlides().append();
    Rectangle2D.Double rec = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 255, 120, 500, 280);
    IAutoShape shape = presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rec);
    shape.getFill().setFillType(FillFormatType.NONE);
    shape.getLine().setWidth(0);
    ParagraphEx para1 = new ParagraphEx();
    PortionEx tr1 = new PortionEx();
    tr1.setText("这是第二页!");
    para1.getTextRanges().append(tr1);
    shape.getTextFrame().getParagraphs().append(para1);
    para1.setAlignment(TextAlignmentType.CENTER);
    tr1.getFill().setFillType(FillFormatType.SOLID);
    tr1.getFill().getSolidColor().setColor(Color.blue);
    shape.getTextFrame().getParagraphs().append(new ParagraphEx());

    //在第一张幻灯片上添加形状
    IAutoShape shape1 = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec);
    shape1.getFill().setFillType(FillFormatType.NONE);
    shape1.getLine().setWidth(0);

    //添加链接到网页的超链接
    ParagraphEx para2 = new ParagraphEx();
    PortionEx tr2 = new PortionEx();
    tr2.setText("点击链接到网页");
    tr2.getClickAction().setAddress("https://www.jianshu.com/");
    para2.getTextRanges().append(tr2);
    shape1.getTextFrame().getParagraphs().append(para2);
    shape1.getTextFrame().getParagraphs().append(new ParagraphEx());

    //添加链接到邮箱地址的超链接
    ParagraphEx para3 = new ParagraphEx();
    PortionEx tr3 = new PortionEx();
    tr3.setText("点击链接到邮箱地址");
    tr3.getClickAction().setAddress("mailto:Tina.tang@e-iceblue.com");
    para3.getTextRanges().append(tr3);
    shape1.getTextFrame().getParagraphs().append(para3);
    shape1.getTextFrame().getParagraphs().append(new ParagraphEx());

    //添加链接到其他文档的超链接
    ParagraphEx para4 = new ParagraphEx();
    PortionEx tr4 = new PortionEx();
    tr4.setText("点击链接到其他文档");
    tr4.getClickAction().setAddress("C:\\Users\\Test1\\Desktop\\月销售统计表.xlsx");
    para4.getTextRanges().append(tr4);
    shape1.getTextFrame().getParagraphs().append(para4);
    shape1.getTextFrame().getParagraphs().append(new ParagraphEx());

    //添加超链接跳转到其他幻灯片
    ParagraphEx para5 = new ParagraphEx();
    PortionEx tr5 = new PortionEx("点击跳转到第二张幻灯片");
    ClickHyperlink link = new ClickHyperlink(presentation.getSlides().get(1));
    tr5.setClickAction(link);
    para5.getTextRanges().append(tr5);
    shape1.getTextFrame().getParagraphs().append(para5);

    //保存文档
    presentation.saveToFile("output/TextHyperlink.pptx", FileFormat.PPTX_2010);
  }
}

添加效果:

注:需幻灯片放映时方能显示超链接地址!

【示例2】添加图片超链接

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;

public class ImageHyperlink {
  public static void main(String[] args) throws Exception {
    //创建Presentation对象
    Presentation presentation = new Presentation();

    //获取第一张幻灯片
    ISlide slide = presentation.getSlides().get(0);

    //添加图片到幻灯片
    String imaPath = "C:\\Users\\Test1\\Desktop\\Signature.png";
    Rectangle2D.Float rect = new Rectangle2D.Float(50, 50, 220, 100);
    IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect);

    //将图片形状的边线设置为无
    image.getLine().setFillType(FillFormatType.NONE);

    //添加超链接到图片
    ClickHyperlink hyperlink = new ClickHyperlink("https://www.jianshu.com/u/96431825b792");
    image.setClick(hyperlink);

    //保存文档
    presentation.saveToFile("output/ImageHyperLink.pptx", FileFormat.PPTX_2013);
  }
}

添加效果:

注:需幻灯片放映时方能显示超链接地址!

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多