目录,是指书籍、文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读、检索内容的作用。在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[].Paragraphs.Insert(, paraInserted);
paraInserted.Format.HorizontalAlignment = HorizontalAlignment.Center;

step3:插入目录

doc.Sections[].Paragraphs[].AppendTOC(,);

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

doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading1);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading1);
doc.Sections[].Paragraphs[].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[].Paragraphs.Insert(, paraInserted);
paraInserted.Format.HorizontalAlignment = HorizontalAlignment.Center; //在第一段添加目录表
doc.Sections[].Paragraphs[].AppendTOC(, ); //设置指定段落的大纲级别
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading1);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading2);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading1);
doc.Sections[].Paragraphs[].ApplyStyle(BuiltinStyle.Heading1); //更新目录
doc.UpdateTableOfContents(); //保存文档
doc.SaveToFile("result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("result.docx");
}
}
}

(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[].Paragraphs.Insert(, paraInserted);
paraInserted.Format.HorizontalAlignment = HorizontalAlignment.Center; //使用域代码自定义目录
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
doc.Sections[].Paragraphs[].AppendTOC(, );
//更新目录
doc.UpdateTableOfContents(); //保存文档
doc.SaveToFile("output.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("output.docx");
}
}
}

二、删除目录

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[].Body; //移除目录
Regex regex = new Regex("TOC\\w+");
for (int i = ; 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");
}
}
}

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

(本文完)

转载注明出处!

C# 操作Word目录——生成、删除目录的更多相关文章

  1. Python操作Word批量生成文章

    需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同也不好.大体看了一下,此类的记录大致分为四段.于是决定每段提供四种选项,每段从四选项里随 ...

  2. Word目录生成

    之所以写这篇文章,是因为每次写报告都需要生成相应目录,但常常只记得个大概,最终还得要重新百度,十分头疼,故在此记录一下. 大概分为3个步骤 步骤1 设置标题级数 进入大纲模式 选择相应级数,这里选的是 ...

  3. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  4. VBA操作word生成sql语句

    项目开始一般都是用word保存下数据库的文档 但是从表单一个一个的建表实在是很困难乏味,查查资料 1.可以生成一个html或者xml,检索结构生成sql.但是这个方式也蛮麻烦 2.查到vba可以操作w ...

  5. Python操作Word【批量生成文章】

    http://www.cnblogs.com/codex/p/4668396.html 需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同 ...

  6. DocX操作word生成报表

    1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...

  7. C# 处理Word自动生成报告 三、设计模板

    C# 处理Word自动生成报告 一.概述 C# 处理Word自动生成报告 二.数据源例子 C# 处理Word自动生成报告 三.设计模板 C# 处理Word自动生成报告 四.程序处理 既然是模板就少不了 ...

  8. C#操作Word Aspose.Words组件介绍及使用—基本介绍与DOM概述

    1.基本介绍 Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务.Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XP ...

  9. Asp.net操作Word文档,原来这么简单啊!

    引用Word对象库文件  具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为 ...

随机推荐

  1. python_code list_2

    >>> import math>>> math.sin(0.5)0.479425538604203>>> >>> import ...

  2. C Primer Plus 第8章 字符输入/输出和验证输入 编程练习

    1. #include <stdio.h> int main(){ char ch; int ct = 0; while ((ch=getchar()) != EOF) ct++; pri ...

  3. PAT1081:Rational Sum

    1081. Rational Sum (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given N ...

  4. PAT1003:Emergency

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  5. Anaconda下载及安装教程

    Anaconda官网 https://www.anaconda.com/download/#windows 选择Python 3.6版本 下一步,选择安装路径 下一步,两个方框打上对号,点击Insta ...

  6. React Native在特赞的应用与实践

    基于React技术栈构建开发前端项目,并使用React Native开发特赞移动APP 目前正在使用Node.js开发和维护特赞服务网关,希望Node.js能够在更轻量级的微服务架构中发挥重要作用 课 ...

  7. 多线程统计次数问题:即count++

    场景:日志需要统计每天数据上传的次数和上传的数据量. 如果是单线程可以使用简单的int count = 0:count++,但很多情况都是多线程环境所以就不能单纯的使用count++了!!! 多线程环 ...

  8. 如何解决python升级后yum报错

    当我们yum命令的时候,会提示 "File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxEr ...

  9. 电脑不识别U盘

    最近遇到这样一个问题,现把具体问题和解决方案给大家分享一下: 系统:win10 症状:插入U盘,系统提示插入U盘的声音提示,磁盘不显示,360 无图标 原因:USB驱动出现问题 对症下药: 插入U盘右 ...

  10. 删除外部dwg中指定的块定义

    本例实现删除外部图纸中指定的块定义,在外部图纸当前模型空间中是没有该块定义的块参照存在. 代码如下: void CBlockUtil::DeleteBlockDefFormOtherDwg(const ...