超链接指的是在word文本或者图片中插入能跳转到其他位置或对象的链接,常见的超链接可以链接到网址、电子邮箱地址、外部文件和书签。本文将介绍如何使用spire.doc删除文本超链接和图片超链接,并保留原来的文本和图片。
源文档:
c#
//创建word对象并加载文档
document document = new document();
document.loadfromfile(@"hyperlinks.docx");
foreach (section section in document.sections)
{
//删除正文里的超链接
foreach (documentobject obj in section.body.childobjects)
{
removelinks(obj,document);
}
//删除页眉页脚中的超链接
foreach (headerfooter hf in section.headersfooters)
{
foreach (documentobject hfobj in hf.childobjects)
{
removelinks(hfobj, document);
}
}
}
//保存文档
document.savetofile("removelinks.docx",fileformat.docx);
private static void removelinks(documentobject obj,document document)
{
//删除段落中的超链接
removelinksinpara(obj,document);
//删除表格中的超链接
if (obj.documentobjecttype == documentobjecttype.table)
{
foreach (tablerow row in (obj as table).rows)
{
foreach (tablecell cell in row.cells)
{
foreach (documentobject cobj in cell.childobjects)
{
removelinksinpara(cobj,document);
}
}
}
}
}
private static void removelinksinpara(documentobject obj,document document)
{
if (obj.documentobjecttype == documentobjecttype.paragraph)
{
var objs = (obj as paragraph).childobjects;
for (int i = 0; i < objs.count; i )
{
if (objs[i].documentobjecttype == documentobjecttype.field)
{
//获取超链接域
field field = objs[i] as field;
if (field.type == fieldtype.fieldhyperlink)
{
//获取超链的文本或图片对象
documentobject dobj = field.nextsibling.nextsibling as documentobject;
//删除文本超链接,保留文本和样式
if (dobj is textrange)
{
//获取超链接文本样式
characterformat format = (dobj as textrange).characterformat;
format.underlinestyle = underlinestyle.none;
format.textcolor = color.black;
//创建textrange并把超链接的文本赋给它
textrange tr = new textrange(document);
tr.text = field.fieldtext;
//应用样式
tr.applycharacterformat(format);
//删除文本超链接域
objs.removeat(i);
//重新插入文本
objs.insert(i, tr);
}
//删除图片超链接,保留图片
if (dobj is docpicture)
{
//删除图片超链接域
objs.removeat(i);
//重新插入图片
objs.insert(i, dobj);
}
}
}
}
}
}
vb.net
'创建word对象并加载文档
dim document as document = new document
document.loadfromfile("hyperlinks.docx")
for each section as section in document.sections
'删除正文里的超链接
for each obj as documentobject in section.body.childobjects
removelinks(obj, document)
next
'删除页眉页脚中的超链接
for each hf as headerfooter in section.headersfooters
for each hfobj as documentobject in hf.childobjects
removelinks(hfobj, document)
next
next
next
'保存文档
document.savetofile("removelinks.docx", fileformat.docx)
private shared sub removelinks(byval obj as documentobject, byval document as document)
'删除段落中的超链接
removelinksinpara(obj, document)
'删除表格中的超链接
if (obj.documentobjecttype = documentobjecttype.table) then
for each row as tablerow in ctype(obj,table).rows
for each cell as tablecell in row.cells
for each cobj as documentobject in cell.childobjects
removelinksinpara(cobj, document)
next
next
next
end if
end sub
private shared sub removelinksinpara(byval obj as documentobject, byval document as document)
if (obj.documentobjecttype = documentobjecttype.paragraph) then
dim objs = ctype(obj,paragraph).childobjects
dim i as integer = 0
do while (i < objs.count)
if (objs(i).documentobjecttype = documentobjecttype.field) then
'获取超链接域
dim field as field = ctype(objs(i),field)
if (field.type = fieldtype.fieldhyperlink) then
'获取超链的文本或图片对象
dim dobj as documentobject = ctype(field.nextsibling.nextsibling,documentobject)
'删除文本超链接,保留文本和样式
if (typeof dobj is textrange) then
'获取超链接文本样式
dim format as characterformat = ctype(dobj,textrange).characterformat
format.underlinestyle = underlinestyle.none
format.textcolor = color.black
'创建textrange并把超链接的文本赋给它
dim tr as textrange = new textrange(document)
tr.text = field.fieldtext
'应用样式
tr.applycharacterformat(format)
'删除文本超链接域
objs.removeat(i)
'重新插入文本
objs.insert(i, tr)
end if
'删除图片超链接,保留图片
if (typeof dobj is docpicture) then
'删除图片超链接域
objs.removeat(i)
'重新插入图片
objs.insert(i, dobj)
end if
end if
end if
i = (i 1)
loop
end if
end sub
结果: