本教程展示了如何使用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("点击访问中文网站k8凯发天生赢家主页");
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("点击发送邮件联系k8凯发天生赢家销售团队");
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);
}
}
效果图如下: