当前位置: 移动技术网 > IT编程>开发语言>.net > C# 操作Word目录——生成、删除目录

C# 操作Word目录——生成、删除目录

2019年02月19日  | 移动技术网IT编程  | 我要评论

抚宁房屋出租,泰剧云上的宝石,青岛电缆

 目录,是指书籍、文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读、检索内容的作用。在word中生成目录前,需要设置文档相应文字或者段落的大纲级别,根据设定的大纲级别可创建文档的交互式大纲,即在word文档左侧导航窗口中可显示为如同目录的标题大纲,通过点击相应级别的内容,可跟踪阅读位置或者快速移动相应的文档内容。下面将介绍如何通过c# 编程操作word目录。

生目录时,这里考虑两种情况:

  • 文档没有设置大纲级别,生成目录时需手动设置
  • 文档已有大纲级别,此时,通过使用域代码来创建目录

使用工具:free spire.doc for .net(免费版)

dll文件引用:

安装后,注意在程序中添加引用spire.doc.dll(dll可在安装路径下的bin文件夹中获取)

一、生成目录

   (1)手动设置大纲级别,生成目录

 step1:加载文档

document doc = new document();
doc.loadfromfile("test.docx");

step2:在文档正文前插入一个新的段落

paragraph parainserted = new paragraph(doc);
textrange textrange = parainserted.appendtext("目 录");
textrange.characterformat.bold = true;
textrange.characterformat.textcolor = color.cadetblue;
doc.sections[0].paragraphs.insert(0, parainserted);
parainserted.format.horizontalalignment = horizontalalignment.center;

step3:插入目录

doc.sections[0].paragraphs[0].appendtoc(1,3);

step4:设置指定段落的大纲级别

doc.sections[0].paragraphs[1].applystyle(builtinstyle.heading1);
doc.sections[0].paragraphs[4].applystyle(builtinstyle.heading2);
doc.sections[0].paragraphs[6].applystyle(builtinstyle.heading2);
doc.sections[0].paragraphs[8].applystyle(builtinstyle.heading2);
doc.sections[0].paragraphs[11].applystyle(builtinstyle.heading1);
doc.sections[0].paragraphs[13].applystyle(builtinstyle.heading1);

step5:更新目录

doc.updatetableofcontents(); 

step6:保存文档

doc.savetofile("result.docx", fileformat.docx2010);

目录生成效果:

全部代码:

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

namespace createtoc_doc
{
    class program
    {
        static void main(string[] args)
        {
            //创建document对象,加载word文档
            document doc = new document();
            doc.loadfromfile("sample.docx");

            //插入一个段落作为第一段
            paragraph parainserted = new paragraph(doc);
            textrange textrange = parainserted.appendtext("目 录");
            textrange.characterformat.bold = true;
            textrange.characterformat.textcolor = color.cadetblue;
            doc.sections[0].paragraphs.insert(0, parainserted);
            parainserted.format.horizontalalignment = horizontalalignment.center;

            //在第一段添加目录表
            doc.sections[0].paragraphs[0].appendtoc(1, 3);           

            //设置指定段落的大纲级别
            doc.sections[0].paragraphs[1].applystyle(builtinstyle.heading1);
            doc.sections[0].paragraphs[4].applystyle(builtinstyle.heading2);
            doc.sections[0].paragraphs[6].applystyle(builtinstyle.heading2);
            doc.sections[0].paragraphs[8].applystyle(builtinstyle.heading2);
            doc.sections[0].paragraphs[11].applystyle(builtinstyle.heading1);
            doc.sections[0].paragraphs[13].applystyle(builtinstyle.heading1);
            
            //更新目录
            doc.updatetableofcontents();          

            //保存文档
            doc.savetofile("result.docx", fileformat.docx2010);
            system.diagnostics.process.start("result.docx");
        }
    }
}
view code

(2)使用域代码生成目录

在(1)中,step3之前添加一个step

tableofcontent toc = new tableofcontent(doc, "{\\o \"1-3\" \\h \\z \\u}");

并省略step4,即可。

目录生成效果:

全部代码:

 

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

namespace createtoc_doc
{
    class program
    {
        static void main(string[] args)
        {
            //创建document对象,加载word文档
            document doc = new document();
            doc.loadfromfile("sample.docx");

            //插入一个段落作为第一段
            paragraph parainserted = new paragraph(doc);
            textrange textrange = parainserted.appendtext("目 录");
            textrange.characterformat.bold = true;
            textrange.characterformat.textcolor = color.cadetblue;
            doc.sections[0].paragraphs.insert(0, parainserted);
            parainserted.format.horizontalalignment = horizontalalignment.center;

            //使用域代码自定义目录
            tableofcontent toc = new tableofcontent(doc, "{\\o \"1-3\" \\h \\z \\u}");
            doc.sections[0].paragraphs[0].appendtoc(1, 3); 
            //更新目录
            doc.updatetableofcontents();

            //保存文档
            doc.savetofile("output.docx", fileformat.docx2010);
            system.diagnostics.process.start("output.docx");
        }
    }
}
view code

二、删除目录

using spire.doc;
using system.text.regularexpressions;

namespace removetoc_doc
{
    class program
    {
        static void main(string[] args)
        {
            //创建document对象,并加载含有目录的word文档 
            document doc = new document();
            doc.loadfromfile("result.docx", fileformat.docx2010);

            //获取body
            body body = doc.sections[0].body;

            //移除目录
            regex regex = new regex("toc\\w+");
            for (int i = 0; i < body.paragraphs.count; i++)
            {
                if (regex.ismatch(body.paragraphs[i].stylename))
                {
                    body.paragraphs.removeat(i);
                    i--;
                }
            }

            //保存文档
            doc.savetofile("removetoc.docx", fileformat.docx2010);
            system.diagnostics.process.start("removetoc.docx");
        }
    }
}

 

运行程序,生成的文档中,目录已经被删除。 

 (本文完)

转载注明出处!

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

相关文章:

验证码:
移动技术网