Java 在PPT中添加文本和圖片超鏈接的實現方法
在文檔中添加超鏈接,可以快速從當前文檔跳轉至指定的網頁或打開指定的外部文件。前文中我們介紹過如何使用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:UsersTest1Desktop月銷售統計表.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:UsersTest1DesktopSignature.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); }}
添加效果:
注:需幻燈片放映時方能顯示超鏈接地址!
總結
到此這篇關于Java 在PPT中添加文本和圖片超鏈接的文章就介紹到這了,更多相關java ppt添加超鏈接內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: