Aspose Word 实现生成目录并且插入文档到目录下

2019-01-26 19:26

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 \\\ \\\\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(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln(\ builder.Writeln(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; builder.Writeln(\ builder.Writeln(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3; builder.Writeln(\ builder.Writeln(\ builder.Writeln(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln(\ builder.Writeln(\

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText; // Call the method below to update the TOC.

doc.UpdateFields(); doc.Save(@\

生成以后把目录文件作为源文件,然后插入文档到目录下: Document mainDoc = new Document(\

Document insertDoc = new Document(\其中insert.doc是要插入的文档 // insertDoc.RemoveUnusedResources();

DocumentBuilder builder = new DocumentBuilder(mainDoc); foreach (Bookmark bm in mainDoc.Range.Bookmarks) {

if (bm.Name.StartsWith(\ {

builder.MoveToBookmark(bm.Name, false, false);

// you can optionally check the style of the current paragraph and then insert document

InsertDocument(bm.BookmarkStart.ParentNode,insertDoc); } }

mainDoc.Save(@\ }

static void InsertDocument(Node insertAfterNode, Document srcDoc) {

// Make sure that the node is either a paragraph or table. if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table)))

throw new ArgumentException(\paragraph or table.\

// We will be inserting into the parent of the destination paragraph. CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.

NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.UseDestinationStyles);

// Loop through all sections in the source document. foreach (Section srcSection in srcDoc.Sections) {

// Loop through all block level nodes (paragraphs and tables) in the body of the section.

foreach (Node srcNode in srcSection.Body) {

// Let's skip the node if it is a last empty paragraph in a section. if (srcNode.NodeType.Equals(NodeType.Paragraph)) {

Paragraph para = (Paragraph)srcNode;

if (para.IsEndOfSection && !para.HasChildNodes) continue; }

// This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = importer.ImportNode(srcNode,true); // Insert new node after the reference node. dstStory.InsertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } }


Aspose Word 实现生成目录并且插入文档到目录下.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:推普周工作总结

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: