Java 添加超链接到 Word 文档方法详解
作者:Jazzz 时间:2023-01-29 08:49:30
在Word文档中,超链接是指在特定文本或者图片中插入的能跳转到其他位置或网页的链接,它也是我们在编辑制作Word文档时广泛使用到的功能之一。今天这篇文章就将为大家演示如何使用Free Spire.Doc for Java在Word文档中添加文本超链接和图片超链接。
Jar包导入
方法一:下载Free Spire.Doc for Java包并解压缩,然后将lib文件夹下的Spire.Doc.jar包作为依赖项导入到Java应用程序中。
方法二:通过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) {
//创建Word文档
Document doc = new Document();
Section section = doc.addSection();
//添加网页链接
Paragraph paragraph = section.addParagraph();
paragraph.appendText("网页链接:");
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);
//创建段落样式
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);
//应用段落样式
section.getParagraphs().get(i).applyStyle(style1.getName());
}
//保存文档
doc.saveToFile("InsertHyperlinks.docx", FileFormat.Docx_2013);
}
}
来源:https://www.cnblogs.com/jazz-z/p/13354674.html
标签:Java,超链接,Word,文档
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android设计模式系列之组合模式
2023-02-09 16:24:19
![](https://img.aspxhome.com/file/2023/2/139142_0s.jpg)
Java详细分析讲解泛型
2023-02-05 07:56:02
![](https://img.aspxhome.com/file/2023/9/71639_0s.png)
Unity实现游戏卡牌滚动效果
2023-09-20 10:54:23
![](https://img.aspxhome.com/file/2023/2/80092_0s.jpg)
C#实现winform自动关闭MessageBox对话框的方法
2022-09-02 02:21:53
android使用AsyncTask实现多线程下载实例
2023-02-02 16:30:04
![](https://img.aspxhome.com/file/2023/3/138373_0s.png)
Java 中的弱引用是什么
2021-08-21 08:10:46
C#操作注册表之Registry类
2023-02-24 00:23:33
Windows下安装ElasticSearch的方法(图文)
2023-11-25 13:35:35
![](https://img.aspxhome.com/file/2023/2/60192_0s.png)
java 按行读取文件并输出到控制台的方法
2022-10-28 22:48:20
SpringCloud eureka(server)微服务集群搭建过程
2023-05-22 15:08:55
![](https://img.aspxhome.com/file/2023/4/69104_0s.jpg)
C#下listview如何插入图片
2023-06-18 11:45:44
Android如何实现锁屏状态下弹窗
2021-05-27 03:38:41
OpenGL Shader实现光照发光体特效
2022-03-16 18:51:50
![](https://img.aspxhome.com/file/2023/5/101865_0s.png)
Android软键盘弹出时的界面控制方法
2022-10-26 03:37:43
Android截屏SurfaceView黑屏问题的解决办法
2023-10-23 11:12:25
C# 获取枚举值的简单实例
2023-09-01 00:13:23
Java原生HttpClient的使用详解
2022-06-04 16:29:49
![](https://img.aspxhome.com/file/2023/7/125257_0s.jpg)
C#实现动态生成静态页面的类详解
2022-03-25 02:25:29
Spring createBeanInstance实例化Bean
2023-06-17 17:26:27
Spring4下validation数据校验无效(maven)的解决
2022-01-24 03:01:04