超链接指的是在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)