目录,是指书籍、文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读、检索内容的作用。在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. Maven学习(四)-- Maven的核心概念

    摘自:http://www.cnblogs.com/xdp-gacl/p/4051819.html 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1. ...

  2. 利用JavaScriptSerializer转json实用方法

    项目中经常碰到需要输出的是json数据,使用JavaScriptSerializer转换,以前老的方法如下. JavaScriptSerializer jss = new JavaScriptSeri ...

  3. PHP之cookies小练习

    //5-1.php 1 <? error_reporting(E_ALL ^ E_NOTICE); if ($_COOKIE['username']!="") { echo ...

  4. .NET面试常考算法

    1.求质数    质数也成为素数,质数就是这个数除了1和他本身两个因数以外,没有其他因数的数,叫做质数,和他相反的是合数,    就是除了1和他本身两个因数以外,还友其他因数的数叫做合数. 1 nam ...

  5. Oracle-09:聚合函数

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 数据库脚本放一下,供测试使用 create table DEPT ( deptno ) not null, d ...

  6. maven使用与技巧

    1.Pom文件介绍与基本组成 说明:全称是Project Object Model,通俗点的话说就是要对构建的项目进行建模. 组成的基本元素: 元素 描述 modelVersion 超级pom版本 g ...

  7. 第七章——集成学习和随机森林(Ensemble Learning and Random Forests)

    俗话说,三个臭皮匠顶个诸葛亮.类似的,如果集成一系列分类器的预测结果,也将会得到由于单个预测期的预测结果.一组预测期称为一个集合(ensemble),因此这一技术被称为集成学习(Ensemble Le ...

  8. 第六章——决策树(Decision Trees)

    决策树是强大的,多功能的机器学习算法. 6.1 训练和可视化一个决策树 在iris数据集训练DecisionTreeClassifier: from sklearn.datasets import l ...

  9. 使用OAuth2的SSO分析

    参考:https://github.com/spring-guides/tut-spring-security-and-angular-js/blob/master/oauth2-vanilla/RE ...

  10. Python+Selenium+PIL+Tesseract真正自动识别验证码进行一键登录

    Python 2.7 IDE Pycharm 5.0.3 Selenium:Selenium的介绍及使用,强烈推荐@ Eastmount的博客 PIL : Pillow-3.3.0-cp27-cp27 ...