Java 添加超连接到Word文档

对特定元素添加超连接后,用户能够经过点击被连接的元素来激活这些连接,一般在被连接的元素下带有下划线或者以不一样的颜色显示来进行区分。按照使用对象的不一样,连接能够分为文本超连接,图像超连接,E-mail连接,锚点连接,多媒体文件连接,空连接等多种连接,本篇文章中将介绍在Word中添加如下几种常见超连接的方法,包括:html

1. 网页连接java

 1.1 给文本添加网页连接app

 1.2 给图片添加网页连接maven

2. 添加文档连接工具

3. E-mail邮箱连接测试

 

使用工具:Free Spire.Doc for Java (免费版)spa

Jar文件获取及导入:code

方法1官网获取jar文件包。下载并解压文件。解压后,将文件夹lib下的Spire.Doc.jar文件导入Java程序。以下:orm

 

 

方法2经过maven仓库安装导入。htm

 

Java代码示例(供参考)

import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.DocPicture; public class AddHyperlink { public static void main(String[]args){ //建立文档
        Document doc = new Document(); Section section = doc.addSection(); //给文字添加网页连接
        Paragraph paragraph = section.addParagraph(); paragraph.appendText("网页连接:"); paragraph.appendHyperlink("https://www.baidu.com/","HomePage", HyperlinkType.Web_Link); //给图片添加网页超连接
        paragraph = section.addParagraph(); paragraph.appendText("图片连接:"); paragraph = section.addParagraph(); DocPicture picture = paragraph.appendPicture("code.png"); picture.setTextWrappingStyle(TextWrappingStyle.Inline); paragraph.appendHyperlink("https://baike.baidu.com/item/Java/85979?fr=aladdin",picture, HyperlinkType.Web_Link); //添加邮箱连接
        paragraph = section.addParagraph(); paragraph.appendText("邮箱连接:"); paragraph.appendHyperlink("mailto:zzhuang@163.com","zzhuang@ 163.com", HyperlinkType.E_Mail_Link); //添加文档连接
        paragraph = section.addParagraph(); paragraph.appendText("文档连接:"); String filePath = "C:\\Users\\Administrator\\Desktop\\测试文档\\sample.docx"; paragraph.appendHyperlink(filePath,"点击查看原文档", HyperlinkType.File_Link); //建立段落样式
        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.Left); //段落末尾自动添加间隔
            section.getParagraphs().get(i).getFormat().setAfterAutoSpacing(true); //应用段落样式
 section.getParagraphs().get(i).applyStyle(style1.getName()); } //保存文档
        doc.saveToFile("AddHyperlinks.docx", FileFormat.Docx_2013); } }

超连接添加效果:

 

(本文完)

 

转载请注明出处!

原文出处:https://www.cnblogs.com/Yesi/p/11640251.html

相关文章
相关标签/搜索