文檔半島外圍網上直營>>E-iceblue中文文檔>>插入和刪除TOC
                插入和刪除TOC
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
我們已經演示了如何在創建 word 文檔時添加一個全新的 TOC 。本文將向您展示如何將 TOC 插入到現有的帶有樣式的 Word 文檔中,并從 Word 文檔中刪除 TOC。
首先,查看帶有 Title、Heading1 和 Heading 2 樣式的示例文檔:
	 
 
下面的代碼片段顯示了如何將目錄 (TOC) 插入到文檔中。
	
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Text.RegularExpressions;
namespace InsertTOC
{
class Program
{
static void Main(string[] args)
{
//Create a new instance of Document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
//Add the TOC to the document
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
Paragraph p = doc.LastSection.Paragraphs[0];
p.Items.Add(toc);
p.AppendFieldMark(FieldMarkType.FieldSeparator);
p.AppendText("TOC");
p.AppendFieldMark(FieldMarkType.FieldEnd);
doc.TOC = toc;
//Update the table of contents
doc.UpdateTableOfContents();
//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx);
}
}
}
	
	 
 
從文檔中刪除目錄
	
using Spire.Doc;
using System.Text.RegularExpressions;
namespace RemoveTOC
{
class Program
{
static void Main(string[] args)
{
//load the document from file with TOC
Document doc = new Document();
doc.LoadFromFile("Result.docx", FileFormat.Docx2010);
//get the first body from the first section
Body body = doc.Sections[0].Body;
//remove TOC from first 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--;
}
}
//save the document to file
doc.SaveToFile("RemoveTOC.docx", FileFormat.Docx2010);
}
}
}
	
	 
 
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線