翻譯|使用教程|編輯:胡濤|2022-11-09 14:00:08.243|閱讀 180 次
概述:本文演示了如何使用Spire.Doc for .NET在 C# 和 VB.NET 中向文本或圖像添加超鏈接,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
Word 文檔中的超鏈接使讀者能夠從其位置跳轉到文檔中的不同位置,或者跳轉到不同的文件或網站,或者跳轉到新的電子郵件消息。超鏈接使讀者可以快速輕松地導航到相關信息。本文演示了如何使用Spire.Doc for .NET在 C# 和 VB.NET 中向文本或圖像添加超鏈接。
首先,您需要將 Spire.Doc for.NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。
PM> Install-Package Spire.Doc
Spire.Doc 提供了Paragraph.AppendHyperlink()方法,用于將 Web 鏈接、電子郵件鏈接、文件鏈接或書簽鏈接添加到段落中的一段文本或圖像。以下是詳細步驟。
【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("http://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("http://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("http://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("http://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
將超鏈接添加到文檔中的現(xiàn)有文本有點復雜。您需要先找到目標字符串,然后在段落中將其替換為超鏈接字段。以下是步驟。
【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 = "http://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 = "http://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 文檔插入超鏈接,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn