本文介紹通過Java程式在PPT幻燈片中添加超連結的方法,可以給文本或者圖檔設定超連結,設定超連結時,可設定包括網頁連結、郵件位址連結、幻燈片跳轉連結等不同指向對象的連結。文中方法使用了免費版PPT類庫(Free Spire.Presentation for Java),可在官網下載下傳jar包,并解壓将lib檔案夾下的jar導入java程式。
如下導入效果:
程式運作環境:Java、IDEA、jdk1.8.0、無需安裝Microsoft PowerPointJava代碼示例
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
public class AddHyperlink {
public static void main(String[] args) throws Exception{
//加載測試PPT
Presentation ppt = new Presentation();
ppt.loadFromFile("test.pptx");
//執行個體化Rectangle2D.Double類的對象
Rectangle2D.Double rec = new Rectangle2D.Double(350, 150, 400, 180);
//在第1張幻燈片上添加形狀
IAutoShape shape1 = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec);
shape1.getFill().setFillType(FillFormatType.NONE);
shape1.getLine().setFillType(FillFormatType.NONE);
//添加連結到網頁的超連結
ParagraphEx para1 = new ParagraphEx();
PortionEx tr1 = new PortionEx();
tr1.setText("1. 網頁位址連結:點選通路網頁");
tr1.getClickAction().setAddress("https://www.baidu.com/");
para1.getTextRanges().append(tr1);
shape1.getTextFrame().getParagraphs().append(para1);
shape1.getTextFrame().getParagraphs().append(new ParagraphEx());
//添加連結到郵箱位址的超連結
ParagraphEx para2 = new ParagraphEx();
PortionEx tr2 = new PortionEx();
tr2.setText("2. 郵箱位址連結:點選發送郵件");
tr2.getClickAction().setAddress("mailto:[email protected]");
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("3. 幻燈片跳轉連結:點選跳轉到第二張幻燈片");
ClickHyperlink link = new ClickHyperlink(ppt.getSlides().get(1));
tr3.setClickAction(link);
para3.getTextRanges().append(tr3);
shape1.getTextFrame().getParagraphs().append(para3);
//添加圖檔到第2張幻燈片,并設定超連結
String imaPath = "pd.png";
Rectangle2D.Float rect = new Rectangle2D.Float(230, 200, 500, 250);
IEmbedImage image = ppt.getSlides().get(1).getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect);
image.getLine().setFillType(FillFormatType.NONE);
ClickHyperlink hyperlink = new ClickHyperlink("https://www.baidu.com/");
image.setClick(hyperlink);
//儲存文檔
ppt.saveToFile("AddHyperlink.pptx", FileFormat.PPTX_2010);
ppt.dispose();
}
}
超連結添加效果可在幻燈片放映中檢視:
文本超連結:
圖檔超連結:
(本文完)