Java 添加超鏈接到 Word 文檔方法詳解
在Word文檔中,超鏈接是指在特定文本或者圖片中插入的能跳轉(zhuǎn)到其他位置或網(wǎng)頁的鏈接,它也是我們在編輯制作Word文檔時廣泛使用到的功能之一。今天這篇文章就將為大家演示如何使用Free Spire.Doc for Java在Word文檔中添加文本超鏈接和圖片超鏈接。
Jar包導(dǎo)入
方法一:下載Free Spire.Doc for Java包并解壓縮,然后將lib文件夾下的Spire.Doc.jar包作為依賴項導(dǎo)入到Java應(yīng)用程序中。
方法二:通過Maven倉庫安裝JAR包,配置pom.xml文件的代碼如下
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>2.7.3</version> </dependency> </dependencies>
Java代碼
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.HyperlinkType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; import com.spire.doc.fields.DocPicture; public class InsertHyperlinks { public static void main(String[] args) { //創(chuàng)建Word文檔 Document doc = new Document(); Section section = doc.addSection(); //添加網(wǎng)頁鏈接 Paragraph paragraph = section.addParagraph(); paragraph.appendText("網(wǎng)頁鏈接:"); paragraph.appendHyperlink("https://www.baidu.com/","主頁", HyperlinkType.Web_Link); //添加郵箱鏈接 paragraph = section.addParagraph(); paragraph.appendText("郵箱鏈接:"); paragraph.appendHyperlink("mailto:xxxxx@163.com","xxxxx@163.com", HyperlinkType.E_Mail_Link); //添加文檔鏈接 paragraph = section.addParagraph(); paragraph.appendText("文檔鏈接:"); String filePath = "C:\\Users\\Administrator\\Desktop\\報表.pdf"; paragraph.appendHyperlink(filePath,"點擊打開報表", HyperlinkType.File_Link); //添加圖片超鏈接 paragraph = section.addParagraph(); paragraph.appendText("圖片鏈接:"); paragraph = section.addParagraph(); DocPicture picture = paragraph.appendPicture("C:\\Users\\Administrator\\IdeaProjects\\Spire.Doc\\logo (2).jpg"); paragraph.appendHyperlink("https://www.baidu.com/",picture, HyperlinkType.Web_Link); //創(chuàng)建段落樣式 ParagraphStyle style1 = new ParagraphStyle(doc); style1.setName("style"); style1.getCharacterFormat().setFontName("宋體"); doc.getStyles().add(style1); for (int i = 0; i < section.getParagraphs().getCount(); i++) { //將段落居中 section.getParagraphs().get(i).getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //段落末尾自動添加間隔 section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true); //應(yīng)用段落樣式 section.getParagraphs().get(i).applyStyle(style1.getName()); } //保存文檔 doc.saveToFile("InsertHyperlinks.docx", FileFormat.Docx_2013); } }
到此這篇關(guān)于Java 添加超鏈接到 Word 文檔方法詳解的文章就介紹到這了,更多相關(guān)Java 添加超鏈接到 Word 文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+hutool批量生成二維碼壓縮導(dǎo)出功能
這篇文章主要介紹了springboot+hutool批量生成二維碼壓縮導(dǎo)出功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10基于Mybatis-plus實現(xiàn)多租戶架構(gòu)的全過程
多租戶是一種軟件架構(gòu)技術(shù),在多用戶的環(huán)境下,共有同一套系統(tǒng),并且要注意數(shù)據(jù)之間的隔離性,下面這篇文章主要給大家介紹了關(guān)于基于Mybatis-plus實現(xiàn)多租戶架構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-02-02Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實URL的示例代碼
本篇文章主要介紹了Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實URL的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式詳析
這篇文章主要給大家介紹了關(guān)于Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式,在編程中我們經(jīng)常需要進(jìn)行各種數(shù)據(jù)類型之間的轉(zhuǎn)換操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10淺談Java中Lock和Synchronized的區(qū)別
這篇文章主要介紹了Java中Lock和Synchronized的區(qū)別,Lock和Synchronized都是java中去用來解決線程安全問題的一個工具,但是具體有什么區(qū)別呢?下面我們一起進(jìn)入文章了解具體詳細(xì)內(nèi)容吧,需要的朋友可以參考一下2022-04-04使用Springboot實現(xiàn)OAuth服務(wù)的示例詳解
OAuth(Open Authorization)是一個開放標(biāo)準(zhǔn),用于授權(quán)第三方應(yīng)用程序訪問用戶資源,而不需要共享用戶憑證。本文主要介紹了如何使用Springboot實現(xiàn)一個OAuth服務(wù),需要的可以參考一下2023-05-05