-
Java 添加超链接到Word文档
对特定元素添加超链接后,用户可以通过点击被链接的元素来激活这些链接,通常在被链接的元素下带有下划线或者以不同的颜色显示来进行区分。按照使用对象的不同,链接可以分为文本超链接,图像超链接,E-mail链接,锚点链接,多媒体文件链接,空链接等多种链接,本篇文章中将介绍在Word中添加以下几种常见超链接的方法,包括:
1. 网页链接
1.1 给文本添加网页链接
1.2 给图片添加网页链接
2. 添加文档链接
3. E-mail邮箱链接
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:官网获取jar文件包。下载并解压文件。解压后,将文件夹lib下的Spire.Doc.jar文件导入Java程序。如下:
方法2:通过maven仓库安装导入。
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
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比