Aspose.Words是一款功能十分强大的word文档处理控件,支持文档的转换、编辑、修改、多个文档的合并,目录书签的生成,文档的插入等很多Office能实现的功能,并且不需要安装office等三方软件,包含Aspose.Words For .NET和Aspose.Words For JAVA。购买正版产品请到控件中国网。
Aspose.Words要实现生成目录并且插入文档到目录下,首先咱们通过下面的代码生成一个目录:// Use a blank document
Document doc = new Document();
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
doc.FirstSection.Body.PrependChild(new Paragraph(doc));
// Move DocumentBuilder cursor to the beginning.
builder.MoveToDocumentStart();
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o\"1-3\"\\h\\z\\u");
// Start the actual document content on the second page.
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");