当前位置: 移动技术网 > IT编程>开发语言>.net > C# 批量删除Word超链接

C# 批量删除Word超链接

2018年09月08日  | 移动技术网IT编程  | 我要评论

柯南和灰原哀h,默言娘子先,新梦下载基地

对于word文档中包含较多的超链接,如果一个个来删除很花费时间和精力,本篇文章将提供一种可用于批量删除word中的超链接的方法。这里的超链接可以是页眉页脚处的超链接、正文中的超链接、表格中的超链接、文字超链接、图片超链接等等。下面将具体演示如何来进行代码操作。

所需工具

ps:下载安装后,注意在项目程序中添加引用spire.doc.dll,dll文件可在安装路径下的bin文件夹中获取。

测试文档中的超链接情况,如下图:

c# 代码示例(供参考)

步骤 1 :添加using指令

using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
using spire.doc.formatting;
using system.drawing;

步骤 2 :加载文件

document document = new document();
document.loadfromfile("sample.docx");

步骤 3 :遍历文档,删除超链接

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);
        }
    }
}

 ps: 这里需要自定义两个方法来分别删除段落中的文字超链接和删除段落中的图片超链接

自定义方法1:

 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);                 
                }
            }
        }
    }
 }           

自定义方法2:

 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);
                       }
                   }
               }
           }
       }
  }

步骤 4 :保存文档

document.savetofile("removelinks.docx", fileformat.docx);
system.diagnostics.process.start("removelinks.docx");

 

调试运行程序,生成文档(如下图)。生成的文档中,原有的超链接将全部被删除。

 

全部代码:

using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
using spire.doc.formatting;
using system.drawing;

namespace removehyperlink_doc
{
    class program
    {
        static void main(string[] args)
        {
            //创建word对象并加载文档
            document document = new document();
            document.loadfromfile("sample.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);
            system.diagnostics.process.start("removelinks.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);
                              }
                          }
                      }
                  }
              }
         }
    }
}
view code

 

 (本文完)

如需转载,请注明出处。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网