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: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("點(diǎn)擊鏈接到其他文檔"); tr4.getClickAction().setAddress("C:\\Users\\Test1\\Desktop\\月銷售統(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對(duì)象 Presentation presentation = new Presentation(); //獲取第一張幻燈片 ISlide slide = presentation.getSlides().get(0); //添加圖片到幻燈片 String imaPath = "C:\\Users\\Test1\\Desktop\\Signature.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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis 中的一對(duì)一,一對(duì)多,多對(duì)多的配置原則示例代碼
這篇文章主要介紹了 Mybatis 中的一對(duì)一,一對(duì)多,多對(duì)多的配置原則示例代碼,需要的朋友可以參考下2017-03-03SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解
這篇文章主要介紹了SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java concurrency之CountDownLatch原理和示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
CountDownLatch是一個(gè)同步輔助類,在完成一組正在其他線程中執(zhí)行的操作之前,它允許一個(gè)或多個(gè)線程一直等待。 下面通過本文給大家分享Java concurrency之CountDownLatch原理和示例,需要的的朋友參考下吧2017-06-06Mybatis-Plus根據(jù)自定義注解實(shí)現(xiàn)自動(dòng)加解密的示例代碼
我們把數(shù)據(jù)存到數(shù)據(jù)庫的時(shí)候,有些敏感字段是需要加密的,從數(shù)據(jù)庫查出來再進(jìn)行解密,如果我們使用的是Mybatis框架,那就跟著一起探索下如何使用框架的攔截器功能實(shí)現(xiàn)自動(dòng)加解密吧,需要的朋友可以參考下2024-06-06maven模塊化開發(fā)部署實(shí)現(xiàn)方案
有些用戶有定制化需求,需要添加新的模塊功能,因此需要平臺(tái)主體功能迭代的同時(shí),非主體功能和定制化功能插件化,本文給大家介紹maven模塊化開發(fā)部署實(shí)現(xiàn)方案,感興趣的朋友一起看看吧2024-01-01