目录,是指书籍、文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读、检索内容的作用。在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. shiro 权限管理配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. Python强大的格式化format

    原文地址:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型 ...

  3. vue项目 构建 打包 发布 三部曲

    一.vue项目的创建 1.首先第一肯定是要有Node.js及npm这个不多说了2.安装脚手架 此时可以直接浏览-但是现在肯定有很多小白想将他发布到gitHub上并可以浏览,使用vue全家桶制作自己的博 ...

  4. flask完成文件上传功能

    在使用flask定义路由完成文件上传时,定义upload视图函数 from flask import Flask, render_template from werkzeug.utils import ...

  5. 第五章 MySQL函数

    一.数学函数 (1) 绝对值函数:ABS(x) ABS(x) 用于返回 x 的绝对值 mysql> SELECT ABS(2), ABS(-2.3), ABS(-33); +--------+- ...

  6. C++ 模板基础

    我们学习使用C++,肯定都要了解模板这个概念.就我自己的理解,模板其实就是为复用而生,模板就是实现代码复用机制的一种工具,它可以实现类型参数化,即把类型定义为参数:进而实现了真正的代码可重用性.模版可 ...

  7. Spring源码阅读笔记

    前言 作为一个Java开发者,工作了几年后,越发觉力有点不从心了,技术的世界实在是太过于辽阔了,接触的东西越多,越感到前所未有的恐慌. 每天捣鼓这个捣鼓那个,结果回过头来,才发现这个也不通,那个也不精 ...

  8. mysql性能调优与架构设计笔记

    1.mysql基本介绍 mysql支持多线程高并发的关系型数据库; 数据库存储引擎InnoDB.MyISAM; mysql快速崛起的原因就是他是开源的; 性能一直是mysql自豪的一大特点; 2.my ...

  9. mybatis数据源源码剖析(JNDI、POOLED、UNPOOLED)

    http://blog.csdn.net/reliveit/article/details/47325189

  10. vue入坑教程(一)

    1.脚手架搭配webpack的安装 (1)需要检查自己的电脑有没有安装node和npm 如果没有安装可以参考官网,以及安装的步骤 官方中文网地址:http://nodejs.cn/ (2)下载webp ...