Word 文档中的超链接可以从超链接位置跳转到文档中的其他位置,或是跳转到不同的文件、网页、以及邮箱地址等。超链接使读者能够快速轻松地导航到相关信息。本文将展示如何使用 Spire.Doc for .NET 通过代码在 Word 文档中插入超链接。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc
添加内容到 Word 文档时插入超链接
Spire.Doc for .NET 提供了 Paragraph.AppendHyperlink() 方法,以将网页链接、邮箱链接、文件链接或书签链接添加到段落中的文本或图像上。以下是添加链接的详细步骤。
- 创建 Document 类的对象。
- 用 Document.AddSection() 方法在文档中添加一个节,并用 Section.AddParagraph() 在节中添加一个段落。
- 用 Paragraph.AppendHyerplink(string link, string text, HyperlinkType type) 方法插入一个文本超链接。
- 用 Paragraph.AppendPicture() 方法在段落中添加一张图片。
- 用 Paragraph.AppendHyerplink(string link, Spire.Doc.Fields.DocPicture picture, HyperlinkType type) 方法插入超链接到图片。
- 用 Document.SaveToFile() 方法保存文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace InsertHyperlinks
{
class Program
{
static void Main(string[] args)
{
//创建Word文档
Document doc = new Document();
//添加一个节
Section section = doc.AddSection();
//添加一个段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendHyperlink("https://www-iceblue.com/", "网站主页", HyperlinkType.WebLink);
//添加换行符
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//添加一个邮箱链接
paragraph.AppendHyperlink("mailto:support @e-iceblue.com", "联系我们", HyperlinkType.EMailLink);
//添加换行符
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//添加一个文件链接
string filePath = @"C:\Users\Administrator\Desktop\report.xlsx";
paragraph.AppendHyperlink(filePath, "点击打开报告", HyperlinkType.FileLink);
//添加换行符
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//另外添加一个节,并创建一个书签
Section section2 = doc.AddSection();
Paragraph bookmarkParagrapg = section2.AddParagraph();
bookmarkParagrapg.AppendText("这是一个书签");
BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("我的书签");
bookmarkParagrapg.Items.Insert(0, start);
bookmarkParagrapg.AppendBookmarkEnd("我的书签");
//链接到书签
paragraph.AppendHyperlink("我的书签", "跳转到本文档中的位置", HyperlinkType.Bookmark);
//添加换行符
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendBreak(BreakType.LineBreak);
//添加一个图片上的超链接
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\dot_net.png");
Spire.Doc.Fields.DocPicture picture = paragraph.AppendPicture(image);
paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink);
//保存文档
doc.SaveToFile("添加超链接.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Namespace InsertHyperlinks
Class Program
Shared Sub Main(ByVal args() As String)
'创建Word文档 Dim doc As Document = New Document() '添加一个节
Dim section As Section = doc.AddSection()
'添加一个段落 Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendHyperlink("https://www-iceblue.com/","网站主页",HyperlinkType.WebLink) '添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
'添加一个邮箱链接 paragraph.AppendHyperlink("mailto:support @e-iceblue.com","联系我们",HyperlinkType.EMailLink) '添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
'添加一个文件链接 Dim filePath As String = "C:\Users\Administrator\Desktop\report.xlsx" paragraph.AppendHyperlink(filePath,"点击打开报告",HyperlinkType.FileLink) '添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
'另外添加一个节,并创建一个书签 Dim section2 As Section = doc.AddSection() Dim bookmarkParagrapg As Paragraph = section2.AddParagraph() bookmarkParagrapg.AppendText("这是一个书签") Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("我的书签") bookmarkParagrapg.Items.Insert(0,start) bookmarkParagrapg.AppendBookmarkEnd("我的书签") '链接到书签
paragraph.AppendHyperlink("我的书签", "跳转到本文档中的位置", HyperlinkType.Bookmark)
'添加换行符 paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) '添加一个图片上的超链接
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\dot_net.png")
Dim picture As Spire.Doc.Fields.DocPicture = paragraph.AppendPicture(image)
paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink)
'保存文档 doc.SaveToFile("添加超链接.docx",FileFormat.Docx2013) End Sub End Class End Namespace
插入超链接到 Word 文档中的已有文本
添加超链接到已有文字需要首先找到目标字符串,然后在段落中用超链接字段替换目标字符串。以下是操作步骤。
- 创建 Document类的对象。
- 用 Document.LoadFromFile()方法载入 Word 文档。
- 用 Document.FindAllString()方法,然后通过集合中的索引获取指定的目标字符串。
- 用 Paragraph.Items.IndexOf()方法获取字符串所在段落及在段落中的位置。
- 用 Paragraph.Items.Remove()方法移除该字符串。
- 创建超链接字段并用 Paragraph.Items.Insert()方法将其插入到原目标字符串所在的位置。
- 用 Document.SaveToFile()方法保存文档。
- C#
- VB.NET
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Interface;namespace AddHyperlinksToExistingText{class Program{static void Main(string[] args){//创建Document类的对象 Document document = new Document();//加载Word文档 document.LoadFromFile(@"C:\Sample.docx");//找到文章中所有的“.NET框架”字符串 TextSelection[] selections = document.FindAllString(".NET框架",true,true);//获取找到的字符串中的第二个 TextRange range = selections[1].GetAsOneRange();//获取该字符串所在的段落 Paragraph parapgraph = range.OwnerParagraph;//获取该字符串在段落中的位置 int index = parapgraph.Items.IndexOf(range);//移除该字符串 parapgraph.Items.Remove(range);//创建超链接字段 Spire.Doc.Fields.Field field = new Spire.Doc.Fields.Field(document);field.Type = Spire.Doc.FieldType.FieldHyperlink;Hyperlink hyperlink = new Hyperlink(field);hyperlink.Type = HyperlinkType.WebLink;hyperlink.Uri = "https://en.wikipedia.org/wiki/.NET_Framework";parapgraph.Items.Insert(index,field);//将字段标识"start"添加到段落中 IParagraphBase start = document.CreateParagraphItem(ParagraphItemType.FieldMark);(start as FieldMark).Type = FieldMarkType.FieldSeparator;parapgraph.Items.Insert(index + 1,start);//在两个字段标示之间插入字符串 ITextRange textRange = new Spire.Doc.Fields.TextRange(document);textRange.Text = ".NET框架";textRange.CharacterFormat.FontName = range.CharacterFormat.FontName;textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;parapgraph.Items.Insert(index + 2,textRange);//将字段标示"end"插入到段落中 IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);(end as FieldMark).Type = FieldMarkType.FieldEnd;parapgraph.Items.Insert(index + 3,end);//保存文档 document.SaveToFile("插入超链接.docx",Spire.Doc.FileFormat.Docx);}}}
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports Spire.Doc.Interface Namespace AddHyperlinksToExistingText Class Program Shared Sub Main(ByVal args() As String) '创建Document类的对象
Dim document As Document = New Document()
'加载Word文档 document.LoadFromFile("C:\Sample.docx") '找到文章中所有的“.NET框架”字符串
Dim selections() As TextSelection = document.FindAllString(".NET框架", True, True)
'获取找到的字符串中的第二个 Dim range As TextRange = selections(1).GetAsOneRange() '获取该字符串所在的段落
Dim parapgraph As Paragraph = range.OwnerParagraph
'获取该字符串在段落中的位置 Dim index As Integer = parapgraph.Items.IndexOf(range) '移除该字符串
parapgraph.Items.Remove(range)
'创建超链接字段 Dim field As Spire.Doc.Fields.Field = New Spire.Doc.Fields.Field(document) field.Type = Spire.Doc.FieldType.FieldHyperlink Dim hyperlink As Hyperlink = New Hyperlink(field) hyperlink.Type = HyperlinkType.WebLink hyperlink.Uri = "https://en.wikipedia.org/wiki/.NET_Framework" parapgraph.Items.Insert(index,field) '将字段标识"start"添加到段落中
Dim start As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)
(start as FieldMark).Type = FieldMarkType.FieldSeparator
parapgraph.Items.Insert(index + 1, start)
'在两个字段标示之间插入字符串 Dim textRange As ITextRange = New Spire.Doc.Fields.TextRange(document) textRange.Text = ".NET框架" textRange.CharacterFormat.FontName = range.CharacterFormat.FontName textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single parapgraph.Items.Insert(index + 2,textRange) '将字段标示"end"插入到段落中
Dim end As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)
(end as FieldMark).Type = FieldMarkType.FieldEnd
parapgraph.Items.Insert(index + 3, end)
'保存文档 document.SaveToFile("插入超链接.docx",Spire.Doc.FileFormat.Docx) End Sub End Class End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。