本教程展示了如何使用Spire.Presentation for Java 向PPT中添加超链接。包括链接到网页,邮箱地址以及其他幻灯片。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class addHyperlink {
public static void main(String[] args) throws Exception {
String outputFile = "output/hyperlink_result.pptx";
//创建一个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("点击了解Spire.Presentation for Java.");
tr2.getClickAction().setAddress("http://www.e-iceblue.cn/Introduce/Spire-Presentation-JAVA.html");
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("https://www.e-iceblue.cn/");
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("https://www.e-iceblue.com/forum/components-f5.html");
para4.getTextRanges().append(tr4);
shape1.getTextFrame().getParagraphs().append(para4);
shape1.getTextFrame().getParagraphs().append(new ParagraphEx());
//添加链接到邮箱地址的超链接
ParagraphEx para5 = new ParagraphEx();
PortionEx tr5 = new PortionEx();
tr5.setText("点击发送邮件联系我们销售团队");
tr5.getClickAction().setAddress("mailto:sales@ e-iceblue.com");
para5.getTextRanges().append(tr5);
shape1.getTextFrame().getParagraphs().append(para5);
shape1.getTextFrame().getParagraphs().append(new ParagraphEx());
//添加超链接跳转到其他幻灯片
ParagraphEx para6 = new ParagraphEx();
PortionEx tr6 = new PortionEx("点击跳转到第二张幻灯片");
ClickHyperlink link = new ClickHyperlink(presentation.getSlides().get(1));
tr6.setClickAction(link);
para6.getTextRanges().append(tr6);
shape1.getTextFrame().getParagraphs().append(para6);
//保存文档
presentation.saveToFile(outputFile, FileFormat.PPTX_2010);
}
}
效果图如下: