本篇文章将介绍通过Spire.Doc for .NET来创建Word目录的方法。这里创建Word目录的方法中需手动设置段落的大纲级别来创建目录;如文档中已设置大纲级别,可通过域代码来创建目录(可参考这篇教程)。 C# //加载源文档 Document doc = new Document(); doc.LoadFromFile("test.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);…