Java 在PPT中添加文本和圖片超鏈接的實(shí)現(xiàn)方法
在文檔中添加超鏈接,可以快速從當(dāng)前文檔跳轉(zhuǎn)至指定的網(wǎng)頁或打開指定的外部文件。前文中我們介紹過如何使用Java程序來為Word文檔和Excel工作表添加超鏈接。本文將演示如何在PPT中添加文本和圖片超鏈接。
使用工具:Free Spire.Presentation for Java(免費(fèi)版)Jar文件獲取及導(dǎo)入:
方法1:通過官網(wǎng)下載獲取jar包。解壓后將lib文件夾下的Spire.Presentation.jar文件導(dǎo)入Java程序。(如下圖)
方法2:通過maven倉庫安裝導(dǎo)入。具體安裝教程參見此網(wǎng)頁。
【示例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 { //創(chuàng)建一個(gè)PPT文檔,默認(rèn)包含一張幻燈片 Presentation presentation = new Presentation(); //在文檔最后追加一張幻燈片并填充一些內(nèi)容,方便之后添加超鏈接鏈接到此幻燈片 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); //添加鏈接到網(wǎng)頁的超鏈接 ParagraphEx para2 = new ParagraphEx(); PortionEx tr2 = new PortionEx(); tr2.setText('點(diǎn)擊鏈接到網(wǎng)頁'); 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('點(diǎn)擊鏈接到郵箱地址'); tr3.getClickAction().setAddress('mailto:[email protected]'); 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('點(diǎn)擊鏈接到其他文檔'); tr4.getClickAction().setAddress('C:UsersTest1Desktop月銷售統(tǒng)計(jì)表.xlsx'); para4.getTextRanges().append(tr4); shape1.getTextFrame().getParagraphs().append(para4); shape1.getTextFrame().getParagraphs().append(new ParagraphEx()); //添加超鏈接跳轉(zhuǎn)到其他幻燈片 ParagraphEx para5 = new ParagraphEx(); PortionEx tr5 = new PortionEx('點(diǎn)擊跳轉(zhuǎn)到第二張幻燈片'); 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); }}
添加效果:
注:需幻燈片放映時(shí)方能顯示超鏈接地址!
【示例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 { //創(chuàng)建Presentation對象 Presentation presentation = new Presentation(); //獲取第一張幻燈片 ISlide slide = presentation.getSlides().get(0); //添加圖片到幻燈片 String imaPath = 'C:UsersTest1DesktopSignature.png'; Rectangle2D.Float rect = new Rectangle2D.Float(50, 50, 220, 100); IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imaPath, rect); //將圖片形狀的邊線設(shè)置為無 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); }}
添加效果:
注:需幻燈片放映時(shí)方能顯示超鏈接地址!
總結(jié)
到此這篇關(guān)于Java 在PPT中添加文本和圖片超鏈接的文章就介紹到這了,更多相關(guān)java ppt添加超鏈接內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: