Word控件Spire.Doc 【超链接】教程(1):如何在C#/VB.NET中给Word 文档插入超链接
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
Spire.Doc for.NET 最新下载(qun:767755948)https://www.evget.com/product/3368/download
Word 文档中的超链接使读者能够从其位置跳转到文档中的不同位置,或者跳转到不同的文件或网站,或者跳转到新的电子邮件消息。超链接使读者可以快速轻松地导航到相关信息。本文演示了如何使用Spire.Doc for .NET在 C# 和 VB.NET 中向文本或图像添加超链接。
步骤一:为 .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 document Document doc = new Document(); //Add a section Section section = doc.AddSection(); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendHyperlink("https://www-iceblue.com/", "Home Page", HyperlinkType.WebLink); //Append line breaks paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendBreak(BreakType.LineBreak); //Add an email link paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink); //Append line breaks paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendBreak(BreakType.LineBreak); //Add a file link string filePath = @"C:\Users\Administrator\Desktop\report.xlsx"; paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink); //Append line breaks paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendBreak(BreakType.LineBreak); //Add another section and create a bookmark Section 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 bookmark paragraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark); //Append line breaks paragraph.AppendBreak(BreakType.LineBreak); paragraph.AppendBreak(BreakType.LineBreak); //Add an image link Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); Spire.Doc.Fields.DocPicture picture = paragraph.AppendPicture(image); paragraph.AppendHyperlink("https://docs.microsoft.com/en-us/dotnet/", picture, HyperlinkType.WebLink); //Save to file doc.SaveToFile("InsertHyperlinks.docx", FileFormat.Docx2013); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports System.Drawing Namespace InsertHyperlinks Class Program Shared Sub Main(ByVal args() As String) 'Create a Word document Document doc = New Document() 'Add a section Dim section As Section = doc.AddSection() 'Add a paragraph Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendHyperlink("https://www.e-iceblue.com/", "Home Page", HyperlinkType.WebLink) 'Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) 'Add an email link paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink) 'Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) 'Add a file link Dim filePath As String = "C:\Users\Administrator\Desktop\report.xlsx" paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink) 'Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) 'Add another section and create a bookmark Dim 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 bookmark paragraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark) 'Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) 'Add an image link Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.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 file doc.SaveToFile("InsertHyperlinks.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("InsertHyperlinks.docx") End Sub End Class End Namespace
步骤三:将超链接添加到 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 object Document document = new Document(); //Load a Word file document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); //Find all the occurrences of the string ".NET Framework" in the document TextSelection[] selections = document.FindAllString(".NET Framework", true, true); //Get the second occurrence TextRange range = selections[1].GetAsOneRange(); //Get its owner paragraph Paragraph parapgraph = range.OwnerParagraph; //Get its position in the paragraph int index = parapgraph.Items.IndexOf(range); //Remove it from the paragraph parapgraph.Items.Remove(range); //Create a hyperlink field 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); //Insert a field mark "start" to the paragraph IParagraphBase start = document.CreateParagraphItem(ParagraphItemType.FieldMark); (start as FieldMark).Type = FieldMarkType.FieldSeparator; parapgraph.Items.Insert(index + 1, start); //Insert a text range between two field marks ITextRange 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 paragraph IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark); (end as FieldMark).Type = FieldMarkType.FieldEnd; parapgraph.Items.Insert(index + 3, end); //Save to file document.SaveToFile("AddHyperlink.docx", Spire.Doc.FileFormat.Docx); } } }
【VB.NET】
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) 'Create a Document object Dim document As Document = New Document() 'Load a Word file document.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx") 'Find all the occurrences of the string ".NET Framework" in the document Dim selections() As TextSelection = document.FindAllString(".NET Framework",True,True) 'Get the second occurrence Dim range As TextRange = selections(1).GetAsOneRange() 'Get its owner paragraph Dim parapgraph As Paragraph = range.OwnerParagraph 'Get its position in the paragraph Dim index As Integer = parapgraph.Items.IndexOf(range) 'Remove it from the paragraph parapgraph.Items.Remove(range) 'Create a hyperlink field 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) 'Insert a field mark "start" to the paragraph Dim start As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark) (start as FieldMark).Type = FieldMarkType.FieldSeparator parapgraph.Items.Insert(index + 1, start) 'Insert a text range between two field marks Dim textRange As ITextRange = 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 paragraph Dim end As IParagraphBase = document.CreateParagraphItem(ParagraphItemType.FieldMark) (end as FieldMark).Type = FieldMarkType.FieldEnd parapgraph.Items.Insert(index + 3, end) 'Save to file document.SaveToFile("AddHyperlink.docx", Spire.Doc.FileFormat.Docx) End Sub End Class End Namespace
以上便是如何在C#/VB.NET中给Word 文档插入超链接,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
相关文章
- 推荐.Net、C# 逆向反编译四大工具利器(请勿用来非法行为)
- c#代码 天气接口 一分钟搞懂你的博客为什么没人看 看完python这段爬虫代码,java流泪了c#沉默了 图片二进制转换与存入数据库相关 C#7.0--引用返回值和引用局部变量 JS直接调用C#后台方法(ajax调用) Linq To Json SqlServer 递归查询
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了 [WPF]为旧版本的应用添加触控支持 js css等静态文件版本控制,一处配置多处更新.net版【原创】 C#图片处理,缩略图制作 SqlDataReader 结果集 转成 DataTable DataTable转成List集合 如何使用linq读取DataTable集合?
- SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型
- 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core
- .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 路由其实也可以很简单-------Asp.net WebAPI学习笔记(一) ASP.NET WebApi技术从入门到实战演练 C#面向服务WebService从入门到精通 DataTable与List<T>相互转换
- .NET C#生成随机颜色,可以控制亮度,生成暗色或者亮色 基于YUV模式判断颜色明亮度
- Word控件Spire.Doc 【脚注】教程(1) 使用C#或VB.NET在 Word 文档中插入脚注和尾注
- Word控件Spire.Doc 【页眉页脚】教程(9): 在 C#、VB.NET 中的 Word 文档之间复制页眉/页脚
- Word控件Spire.Doc 【评论】教程(3):在C#、VB.NET中从Word文档中提取注释并保存在TXT文件中
- Word控件Spire.Doc 【文档操作】教程(七):在 C#、VB.NET 中从 URL 下载 Word 文档
- Word控件Spire.Doc 【文档操作】教程(一):在 C#、VB.NET 中创建、编写和保存 word
- Word控件Spire.Doc 转换教程(三十):C#、VB.NET 在 Azure 应用程序中将 Word 转换为 PDF
- Word控件Spire.Doc 转换教程(二十六):在 C#、VB.NET 中将 Word 转换为 EPUB 时添加封面图片
- Word控件Spire.Doc 转换教程(十四):使如何在 C#、VB.NET 中将 HTML 转换为 XML
- Word处理控件Aspose.Words功能演示:在 ASP.NET 中使用 C# 比较两个 Word 文档
- [.NET]c#.net程序中使用ffmpeg.exe来处理视频并生成上传视频的截图
- asp.net-Creating JavaScript objects from ASP.NET objects
- C#-asp.net-获取当前,相对,绝对路径
- C#.Net遍历远程文件夹下的所有文件 FTP
- C#与.NET Framework c#编程语言,和java是一样的。(c#,java) -->javaweb,asp.net