生目录时,这里考虑两种情况:
- 文档没有设置大纲级别,生成目录时需手动设置
- 文档已有大纲级别,此时,通过使用域代码来创建目录
使用工具:Free Spire.Doc for .NET(免费版)
dll文件引用:
安装后,注意在程序中添加引用Spire.Doc.dll(dll可在安装路径下的bin文件夹中获取)
一、生成目录
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"); } } }
(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"); } } }
二、删除目录
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"); } } }
转载注明出处!