文章插图
步骤三:将超链接添加到 Word 中的现有文本
将超链接添加到文档中的现有文本有点复杂 。您需要先找到目标字符串,然后在段落中将其替换为超链接字段 。以下是步骤 。
【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:\Users\Administrator\Desktop\sample.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:\Users\Administrator\Desktop\sample.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
- word填充颜色 word填充颜色深色25%
- 未来星计算机答案,这些年律师百思不解的10个Word难题,答案全在这里了!
- Word公式编辑器在哪里
- Word怎么横向排版
- Word表格怎么调整行高间距
- Word中如何设置文本轮廓
- 如何裁剪Word中的图片
- 如何对Word表格进行快速计算
- Word中输入网址如何取消设置超链接
- Word怎么设置首字下沉效果