对特定元素添加超链接后,用户可以通过点击被链接的元素来激活这些链接,通常在被链接的元素下带有下划线或者以不同的颜色显示来进行区分。按照使用对象的不同,链接可以分为文本超链接,图像超链接,E-mail链接,锚点链接,多媒体文件链接,空链接等多种链接,本篇文章中将介绍在Word中添加以下几种常见超链接的方法,包括:
1. 网页链接
1.1 给文本添加网页链接
1.2 给图片添加网页链接
2. 添加文档链接
3. E-mail邮箱链接
如果需要修改Word文档中已有的超链接,也可以参考文中的方法。
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:官网获取jar文件包。下载并解压文件。解压后,将文件夹lib下的Spire.Doc.jar文件导入Java程序。如下:
方法2:通过maven仓库安装导入。
Java代码示例(供参考)
【示例1】添加Word超链接
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:[email protected]","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);
}
}超链接添加效果:
【示例2】修改Word中的超链接
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;
import com.spire.doc.Section;
import java.util.ArrayList;
import java.util.List;
public class ModifyHyperlink {
public static void main(String[] args) {
//加载Word文档
Document doc = new Document();
doc.loadFromFile("test.docx");
//获取section
Section section = doc.getSections().get(0);
//创建超链接数组
List<Field> hyperlinks = new ArrayList<Field>();
//遍历段落及段落中的对象
for (Paragraph para : (Iterable<Paragraph>) section.getParagraphs()) {
for (DocumentObject obj:(Iterable<DocumentObject>) 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).setFieldText("新超链接文本");
hyperlinks.get(0).setCode("HYPERLINK \"http://www.baidu.com\"");
//保存文档
doc.saveToFile("ModifyHyperlink.docx", FileFormat.Docx_2013);
doc.dispose();
}
}超链接修改效果:
修改前:
修改后:
(本文完)
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦



