java word 超链接到文档内部_Java 添加超链接到 Word 文档方法详解

在Word文档中,超链接是指在特定文本或者图片中插入的能跳转到其他位置或网页的链接,它也是我们在编辑制作Word文档时广泛使用到的功能之一。今天这篇文章就将为大家演示如何使用Free Spire.Doc for Java在Word文档中添加文本超链接和图片超链接。

Jar包导入

方法一:下载Free Spire.Doc for Java包并解压缩,然后将lib文件夹下的Spire.Doc.jar包作为依赖项导入到Java应用程序中。

方法二:通过Maven仓库安装JAR包,配置pom.xml文件的代码如下

com.e-iceblue

http://repo.e-iceblue.cn/repository/maven-public/

e-iceblue

spire.doc.free

2.7.3

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);

}

}

j2ewmqxpeip.jpg

到此这篇关于Java 添加超链接到 Word 文档方法详解的文章就介绍到这了,更多相关Java 添加超链接到 Word 文档内容请搜索聚米学院以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚米学院!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值