在 Word 中可以对特定元素,如文本或图片,添加超链接,用户可以通过点击被链接的元素来激活这些链接,通常在被链接的元素下带有下划线或者以不同的颜色显示来进行区分。常见的添加的超链接可以链接到网页地址、链接到邮箱地址、链接到文档路径等。下面介绍如何使用 Spire.Doc for Java 给文本和图片添加超链接。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 这个链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.2.1</version>
</dependency>
</dependencies>
添加超链接
Spire.Doc for Java 中的 Paragraph 类提供了 Paragraph.appendHyperlink() 方法用于添加文本超链接和图片超链接。添加的链接包括 HyperlinkType.Web_Link 网页链接、HyperlinkType.E_Mail_Link 邮件链接以及 HyperlinkType.File_Link 文档链接等多种类型。下面是实现超链接添加的详细步骤:
- 实例化 Document 类的对象。
- 调用 Document.addSection() 方法添加节。
- 通过 Section. addParagraph() 方法添加段落。
- 通过 Paragraph.appendHyperlink(java.lang.String link, java.lang.String text, HyperlinkType. HyperlinkType.Web_Link) 方法给文字添加指向网址的超链接。
- 通过 Paragraph.appendHyperlink(java.lang.String link, java.lang.String text, HyperlinkType. HyperlinkType. E_Mail_Link) 方法为文字添加指向邮件地址的超链接。
- 通过 Paragraph.appendHyperlink(java.lang.String link, java.lang.String text, HyperlinkType. HyperlinkType. File_Link) 方法给文字添加指向文档的超链接。
- 通过 Paragraph. appendHyperlink(java.lang.String link, DocPicture picture, HyperlinkType. Web_Link) 方法给图片添加指向网址的超链接。
- 实例化 ParagraphStyle 类的对象,用于创建段落样式。
- 通过for循环遍历每一个段落,并通过 Paragraph.applyStyle() 方法应用段落样式。
- 最后,通过 Document.saveToFile() 方法保存文档。
- 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.e-iceblue.com/","主页", HyperlinkType.Web_Link);
//添加邮箱链接
paragraph = section.addParagraph();
paragraph.appendText("邮箱链接:");
paragraph.appendHyperlink("mailto:support@ e-iceblue.com","support@ e-iceblue.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\\Desktop\\logo-2.png");
paragraph.appendHyperlink("https://www.e-iceblue.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);
}
}
申请临时License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期30天的临时许可证。