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

C#  *** 作Word目录——生成、删除目录,第1张

概述目录,是指书籍、文档正文前所载的目次,将主要内容以一定次第顺序编排,起指导阅读、检索内容的作用。在Word中生成目录前,需要设置文档相应文字或者段落的大纲级别,根据设定的大纲级别可创建文档的交互式大纲

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

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

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

使用工具:Free Spire.Doc for .NET(免费版)

dll文件引用:

安装后,注意在程序中添加引用Spire.Doc.dll(dll可在@R_419_6397@下的bin文件夹中获取)

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

 step1:加载文档

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

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

Paragraph paraInserted =  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[1].ApplyStyle(BuiltinStyle.heading1);doc.Sections[4].ApplyStyle(BuiltinStyle.heading2);doc.Sections[681113].ApplyStyle(BuiltinStyle.heading1);

step5:更新目录

doc.UpdatetableOfContents(); 

step6:保存文档

doc.Savetofile(result.docx",fileFormat.Docx2010);

目录生成效果:

全部代码:

using Spire.Doc; Spire.Doc.documents; Spire.Doc.FIElds; System.Drawing;namespace Createtoc_Doc{    class Program    {        static voID Main(string[] args)        {            //创建document对象,加载Word文档            document doc =  document();            doc.LoadFromfile(sample.docx);            插入一个段落作为第一段            Paragraph paraInserted =  Paragraph(doc);            TextRange textRange = paraInserted.AppendText();            textRange.CharacterFormat.Bold = ;            textRange.CharacterFormat.Textcolor = color.CadetBlue;            doc.Sections[ HorizontalAlignment.Center;            在第一段添加目录表            doc.Sections[3);                       设置指定段落的大纲级别            doc.Sections[].ApplyStyle(BuiltinStyle.heading1);            doc.Sections[].ApplyStyle(BuiltinStyle.heading2);            doc.Sections[13].ApplyStyle(BuiltinStyle.heading1);                        更新目录            doc.UpdatetableOfContents();                      保存文档            doc.Savetofile();        }    }}
VIEw Code(2)使用域代码生成目录

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

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

并省略step4,即可。

目录生成效果:

全部代码:

 

使用域代码自定义目录            tableOfContent toc = );            doc.Sections[);                         doc.UpdatetableOfContents();            output.docx);        }    }}
VIEw Code二、删除目录
 System.Text.RegularExpressions; RemovetoC_Doc{    创建document对象,并加载含有目录的Word文档             document doc = Result.docx获取body            Body body = doc.Sections[].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--;                }            }            RemovetoC.docx);        }    }}

 

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

 (本文完)

转载注明出处!

总结

以上是内存溢出为你收集整理的C# *** 作Word目录——生成、删除目录全部内容,希望文章能够帮你解决C# *** 作Word目录——生成、删除目录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1213391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存