亲宝软件园·资讯

展开

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

Tina_Tang 人气:1

在文档中添加超链接,可以快速从当前文档跳转至指定的网页或打开指定的外部文件。前文中我们介绍过如何使用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);
  }
}

添加效果:

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

总结

加载全部内容

相关教程
猜你喜欢
用户评论