超链接指的是在Word文本或者图片中插入能跳转到其他位置或对象的链接,常见的超链接可以链接到网址、电子邮箱地址、外部文件和书签。本文将介绍如何使用Spire.Doc添加文本超链接和图片超链接,以及如何更改文本超链接的格式。
添加文本超链接
C#
//创建一个Document实例并添加section
Document doc = new Document();
Section section = doc.AddSection();
//添加指向网址的超链接
Paragraph para1 = section.AddParagraph();
para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com",HyperlinkType.WebLink);
//添加指向邮件地址的超链接
Paragraph para2 = section.AddParagraph();
para2.AppendHyperlink("mailto:support @ e-iceblue.com", "support @ e-iceblue.com",HyperlinkType.EMailLink);
//添加指向外部文件的超链接
Paragraph para3 = section.AddParagraph();
string filePath = @"C:\Users\Administrator\Desktop\月销售统计表.xls";
para3.AppendHyperlink(filePath, "点击此处打开另一个文档",HyperlinkType.FileLink);
//设置段落之间的间距
para1.Format.AfterSpacing = 15f;
para2.Format.AfterSpacing = 15f;
//保存文档
doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013);
VB.NET
'创建一个Document实例并添加section
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
'添加指向网址的超链接
Dim para1 As Paragraph = section.AddParagraph
para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink)
'添加指向邮件地址的超链接
Dim para2 As Paragraph = section.AddParagraph
para2.AppendHyperlink("mailto:support @ e-iceblue.com", "support @ e-iceblue.com", HyperlinkType.EMailLink)
'添加指向外部文件的超链接
Dim para3 As Paragraph = section.AddParagraph
Dim filePath As String = "C:\Users\Administrator\Desktop\月销售统计表.xls"
para3.AppendHyperlink(filePath, "点击此处打开另一个文档", HyperlinkType.FileLink)
'设置段落之间的间距
para1.Format.AfterSpacing = 15!
para2.Format.AfterSpacing = 15!
'保存文档
doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013)
添加图片超链接
C#
//创建一个Document实例并添加section
Document doc = new Document();
Section section = doc.AddSection();
//添加段落
Paragraph para = section.AddParagraph();
//添加图片到段落并插入网站链接
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
Spire.Doc.Fields.DocPicture picture = para.AppendPicture(image);
para.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink);
//保存文档
doc.SaveToFile("图片超链接.docx", FileFormat.Docx2013);
VB.NET
'创建一个Document实例并添加section
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
'添加段落
Dim para As Paragraph = section.AddParagraph
'添加图片到段落并插入网站链接
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png")
Dim picture As Spire.Doc.Fields.DocPicture = para.AppendPicture(image)
para.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink)
'保存文档
doc.SaveToFile("图片超链接.docx", FileFormat.Docx2013)
格式化文本超链接
默认状态下,超文本链接通常显示为蓝色带下划线。为了让超链接更醒目,我们也可以更改超链接的显示形式,例如去掉下划线、改变文本颜色、设置字体大小。
C#
//创建一个Document实例并添加section
Document doc = new Document();
Section section = doc.AddSection();
//添加段落
Paragraph para = section.AddParagraph();
//添加超链接到段落并返回到一个TextRange对象
TextRange txtRange = para.AppendHyperlink("www.e-iceblue.com", "WWW.E-ICEBLUE.COM", HyperlinkType.WebLink);
//在TextRange中设置字体名字、大小、颜色、样式
txtRange.CharacterFormat.FontName = "黑体";
txtRange.CharacterFormat.TextColor = Color.Purple;
txtRange.CharacterFormat.FontSize = 15;
txtRange.CharacterFormat.Bold = true;
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
//保存文档
doc.SaveToFile("格式化超链接.docx", FileFormat.Docx2013);
VB.NET
'创建一个Document实例并添加section
Dim doc As Document = New Document
Dim section As Section = doc.AddSection
'添加段落
Dim para As Paragraph = section.AddParagraph
'添加超链接到段落并返回到一个TextRange对象
Dim txtRange As TextRange = para.AppendHyperlink("www.e-iceblue.com", "WWW.E-ICEBLUE.COM", HyperlinkType.WebLink)
'在TextRange中设置字体名字、大小、颜色、样式
txtRange.CharacterFormat.FontName = "黑体"
txtRange.CharacterFormat.TextColor = Color.Purple
txtRange.CharacterFormat.FontSize = 15
txtRange.CharacterFormat.Bold = true
txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None
'保存文档
doc.SaveToFile("格式化超链接.docx", FileFormat.Docx2013)