Word控件Spire.Doc 【超链接】教程(1):如何在C#/VB.NET中给Word 文档插入超链接

步骤一:为 .NET 安装 Spire.Doc

首先,您需要将 Spire.Doc for.NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。

PM> Install-Package Spire.Doc
步骤二:向 Word 添加段落时插入超链接

Spire.Doc 提供了Paragraph.AppendHyperlink()方法,用于将 Web 链接、电子邮件链接、文件链接或书签链接添加到段落中的一段文本或图像。以下是详细步骤。

  • 创建一个文档对象。
  • 向其中添加一个部分和一个段落。
  • 使用Paragraph.AppendHyerplink(string link, string text, HyperlinkType type)方法插入基于文本的超链接。
  • 使用Paragraph.AppendPicture()方法将图像添加到段落。
  • 使用Paragraph.AppendHyerplink(string link, Spire.Doc.Fields.DocPicture picture, HyperlinkType type)方法插入基于图像的超链接。
  • 使用Document.SaveToFile()方法保存文档。

【C#】

using Spire.Doc;using Spire.Doc.Documents;using System.Drawing;namespace InsertHyperlinks{class Program{static void Main(string[] args){//Create a Word documentDocument doc = new Document();//Add a sectionSection section = doc.AddSection();//Add a paragraphParagraph paragraph = section.AddParagraph();paragraph.AppendHyperlink("https://www-iceblue.com/", "Home Page", HyperlinkType.WebLink);//Append line breaksparagraph.AppendBreak(BreakType.LineBreak);paragraph.AppendBreak(BreakType.LineBreak);//Add an email linkparagraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink);//Append line breaksparagraph.AppendBreak(BreakType.LineBreak);paragraph.AppendBreak(BreakType.LineBreak);//Add a file linkstring filePath = @"C:UsersAdministratorDesktopreport.xlsx";paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink);//Append line breaksparagraph.AppendBreak(BreakType.LineBreak);paragraph.AppendBreak(BreakType.LineBreak);//Add another section and create a bookmarkSection section2 = doc.AddSection();Paragraph bookmarkParagrapg = section2.AddParagraph();bookmarkParagrapg.AppendText("Here is a bookmark");BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("myBookmark");bookmarkParagrapg.Items.Insert(0, start);bookmarkParagrapg.AppendBookmarkEnd("myBookmark");//Link to the bookmarkparagraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark);//Append line breaksparagraph.AppendBreak(BreakType.LineBreak);paragraph.AppendBreak(BreakType.LineBreak);//Add an image linkImage image = Image.FromFile(@"C:UsersAdministratorDesktoplogo.png");Spire.Doc.Fields.DocPicture picture = paragraph.AppendPicture(image);paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink);//Save to filedoc.SaveToFile("InsertHyperlinks.docx", FileFormat.Docx2013);}}}

【VB.NET】

Imports Spire.DocImports Spire.Doc.DocumentsImports System.DrawingNamespace InsertHyperlinksClass ProgramShared Sub Main(ByVal args() As String)'Create a Word documentDocument doc = New Document()'Add a sectionDim section As Section = doc.AddSection()'Add a paragraphDim paragraph As Paragraph = section.AddParagraph()paragraph.AppendHyperlink("https://www.e-iceblue.com/", "Home Page", HyperlinkType.WebLink)'Append line breaksparagraph.AppendBreak(BreakType.LineBreak)paragraph.AppendBreak(BreakType.LineBreak)'Add an email linkparagraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink)'Append line breaksparagraph.AppendBreak(BreakType.LineBreak)paragraph.AppendBreak(BreakType.LineBreak)'Add a file linkDim filePath As String = "C:UsersAdministratorDesktopreport.xlsx"paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink)'Append line breaksparagraph.AppendBreak(BreakType.LineBreak)paragraph.AppendBreak(BreakType.LineBreak)'Add another section and create a bookmarkDim section2 As Section = doc.AddSection()Dim bookmarkParagrapg As Paragraph = section2.AddParagraph()bookmarkParagrapg.AppendText("Here is a bookmark")Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("myBookmark")bookmarkParagrapg.Items.Insert(0, start)bookmarkParagrapg.AppendBookmarkEnd("myBookmark")'Link to the bookmarkparagraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark)'Append line breaksparagraph.AppendBreak(BreakType.LineBreak)paragraph.AppendBreak(BreakType.LineBreak)'Add an image linkDim image As Image = Image.FromFile("C:UsersAdministratorDesktoplogo.png")Dim picture As Spire.Doc.Fields.DocPicture = paragraph.AppendPicture(image)paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink)'Save to filedoc.SaveToFile("InsertHyperlinks.docx", FileFormat.Docx2013)System.Diagnostics.Process.Start("InsertHyperlinks.docx")End SubEnd ClassEnd Namespace

C#/VB.NET:向 Word 文档插入超链接
步骤三:将超链接添加到 Word 中的现有文本

将超链接添加到文档中的现有文本有点复杂。您需要先找到目标字符串,然后在段落中将其替换为超链接字段。以下是步骤。

  • 创建一个文档对象。
  • 使用Document.LoadFromFile()方法加载 Word 文件。
  • 使用Document.FindAllString()方法查找文档中所有出现的目标字符串,并从集合中通过其索引获取特定字符串。
  • 获取字符串自己的段落及其在其中的位置。
  • 从段落中删除字符串。
  • 创建一个超链接字段并将其插入到字符串所在的位置。
  • 使用Document.SaveToFle()方法将文档保存到另一个文件。

【C#】

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Interface;namespace AddHyperlinksToExistingText{class Program{static void Main(string[] args){//Create a Document objectDocument document = new Document();//Load a Word filedocument.LoadFromFile(@"C:UsersAdministratorDesktopsample.docx");//Find all the occurrences of the string ".NET Framework" in the documentTextSelection[] selections = document.FindAllString(".NET Framework", true, true);//Get the second occurrenceTextRange range = selections[1].GetAsOneRange();//Get its owner paragraphParagraph parapgraph = range.OwnerParagraph;//Get its position in the paragraphint index = parapgraph.Items.IndexOf(range);//Remove it from the paragraphparapgraph.Items.Remove(range);//Create a hyperlink fieldSpire.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);//Insert a field mark "start" to the paragraphIParagraphBase start = document.CreateParagraphItem(ParagraphItemType.FieldMark);(start as FieldMark).Type = FieldMarkType.FieldSeparator;parapgraph.Items.Insert(index + 1, start);//Insert a text range between two field marksITextRange textRange = new Spire.Doc.Fields.TextRange(document);textRange.Text = ".NET Framework";textRange.CharacterFormat.Font = range.CharacterFormat.Font;textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;parapgraph.Items.Insert(index + 2, textRange);//Insert a field mark "end" to the paragraphIParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);(end as FieldMark).Type = FieldMarkType.FieldEnd;parapgraph.Items.Insert(index + 3, end);//Save to filedocument.SaveToFile("AddHyperlink.docx", Spire.Doc.FileFormat.Docx);}}}

【VB.NET】

Imports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports Spire.Doc.InterfaceNamespace AddHyperlinksToExistingTextClass ProgramShared Sub Main(ByVal args() As String)'Create a Document objectDim document As Document = New Document()'Load a Word filedocument.LoadFromFile("C:UsersAdministratorDesktopsample.docx")'Find all the occurrences of the string ".NET Framework" in the documentDim selections() As TextSelection = document.FindAllString(".NET Framework",True,True)'Get the second occurrenceDim range As TextRange = selections(1).GetAsOneRange()'Get its owner paragraphDim parapgraph As Paragraph = range.OwnerParagraph'Get its position in the paragraphDim index As Integer = parapgraph.Items.IndexOf(range)'Remove it from the paragraphparapgraph.Items.Remove(range)'Create a hyperlink fieldDim field As Spire.Doc.Fields.Field = New Spire.Doc.Fields.Field(document)field.Type = Spire.Doc.FieldType.FieldHyperlinkDim hyperlink As Hyperlink = New Hyperlink(field)hyperlink.Type = HyperlinkType.WebLinkhyperlink.Uri = "https://en.wikipedia.org/wiki/.NET_Framework"parapgraph.Items.Insert(index, field)'Insert a field mark "start" to the paragraphDim start As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)(start as FieldMark).Type = FieldMarkType.FieldSeparatorparapgraph.Items.Insert(index + 1, start)'Insert a text range between two field marksDim textRange As ITextRange = New Spire.Doc.Fields.TextRange(document)textRange.Text = ".NET Framework"textRange.CharacterFormat.Font = range.CharacterFormat.FonttextRange.CharacterFormat.TextColor = System.Drawing.Color.BluetextRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Singleparapgraph.Items.Insert(index + 2, textRange)'Insert a field mark "end" to the paragraphDim end As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark)(end as FieldMark).Type = FieldMarkType.FieldEndparapgraph.Items.Insert(index + 3, end)'Save to filedocument.SaveToFile("AddHyperlink.docx", Spire.Doc.FileFormat.Docx)End SubEnd ClassEnd Namespace

C#/VB.NET:向 Word 文档插入超链接

以上便是如何在C#/VB.NET中给Word 文档插入超链接,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年10月6日
下一篇 2022年10月6日

相关推荐

发表回复

登录后才能评论