C# 操作Word目錄——生成、刪除目錄


 目錄,是指書籍、文檔正文前所載的目次,將主要內容以一定次第順序編排,起指導閱讀、檢索內容的作用。在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[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");
        }
    }
}
View Code

(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");
        }
    }
}
View Code

二、刪除目錄

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");
        }
    }
}

 

運行程序,生成的文檔中,目錄已經被刪除。 

 (本文完)

轉載注明出處!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM