java word 超链接到文档内部_Java 添加、修改Word超链接

本文介绍如何使用Java程序(Spire.Doc库)在Word文档中添加和修改超链接,包括网页链接、邮箱链接、文档链接和图片链接,并提供详细代码示例。
摘要由CSDN通过智能技术生成

在日常使用Word编辑文档时,有时需通过某些内容链接到其他内容,比如链接到特定的段落,图片或其他的文档,甚至是网页或邮箱地址。通过点击这些超链接,可以快速从当前文档跳转至指定的网页或打开指定的外部文件。本文将通过使用Java程序来演示如何在Word文档中添加和修改超链接。

Jar文件获取及导入:

方法1:通过官网下载获取jar包。解压后将lib文件夹下的Spire.Doc.jar文件导入Java程序。(如下图)

6a4cd5e3b18159f0a74b03ed270b4021.png

方法2:通过maven仓库安装导入。具体安装教程详见此网页。

【示例1】添加超链接

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 InsertHyperlink {

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.jianshu.com/u/96431825b792","个人主页", HyperlinkType.Web_Link);

//添加邮箱链接

paragraph = section.addParagraph();

paragraph.appendText("邮箱链接:");

paragraph.appendHyperlink("mailto:2540321664@qq.com","2540321664@qq.com", HyperlinkType.E_Mail_Link);

//添加文档链接

paragraph = section.addParagraph();

paragraph.appendText("文档链接:");

String filePath = "C:\\Users\\Test1\\Desktop\\财务预算.xlsx";

paragraph.appendHyperlink(filePath,"点击查看财务预算", HyperlinkType.File_Link);

//添加图片超链接

paragraph = section.addParagraph();

paragraph.appendText("图片链接:");

paragraph = section.addParagraph();

DocPicture picture = paragraph.appendPicture("C:\\Users\\Test1\\Desktop\\签名.png");

paragraph.appendHyperlink("https://www.jianshu.com/u/96431825b792",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("output/InsertHyperlinks.docx", FileFormat.Docx_2013);

}

}

添加效果:

84459ecb3343662c5fe3846f240792ee.png

【示例2】修改超链接

原文档如下:

a5a4aa203a5c683db6ddbd16d4f6e14c.png

代码示例:

import com.spire.doc.*;

import com.spire.doc.documents.*;

import com.spire.doc.fields.Field;

import java.util.ArrayList;

import java.util.List;

public class EditHyperlink {

public static void main(String[] args) {

//加载Word文档

Document doc = new Document();

doc.loadFromFile("C\\Users\\Test1\\Desktop\\Sample.docx");

List hyperlinks = new ArrayList();

//遍历文档中的节

for (Section section : (Iterable extends Section>) doc.getSections()

) {

//遍历每个节中的段落

for (Paragraph para : (Iterable) section.getParagraphs()

) {

for (DocumentObject obj:(Iterable) para.getChildObjects()

) {

//找到超链接并将其添加至list中

if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) {

Field field = (Field) obj;

if (field.getType().equals(FieldType.Field_Hyperlink)) {

hyperlinks.add(field);

}

}

}

}

}

//修改第一个超链接的展示文字和链接地址

hyperlinks.get(0).setCode("HYPERLINK \"https://www.zhihu.com/topic/19649017/hot");

hyperlinks.get(0).setFieldText("徐志摩(现代新月派代表诗人)");

//保存文档

doc.saveToFile("output/EditHyperlink.docx", FileFormat.Docx_2013);

}

}

修改效果:

040e000534ead2ebf184ac29139abe96.png

(本文完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值