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

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

举报
开通vip

Aspose Word 实现生成目录并且插入文档到目录下Aspose.Words是一款功能十分强大的word文档处理控件,支持文档的转换、编辑、修改、多个文档的合并,目录书签的生成,文档的插入等很多Office能实现的功能,并且不需要安装office等三方软件,包含Aspose.Words For .NET和Aspose.Words For JAVA。购买正版产品请到控件中国网。 Aspose.Words要实现生成目录并且插入文档到目录下,首先咱们通过下面的代码生成一个目录: // Use a blank document Document doc = new D...

Aspose Word 实现生成目录并且插入文档到目录下
Aspose.Words是一款功能十分强大的word文档处理控件,支持文档的转换、编辑、修改、多个文档的合并,目录 关于书的成语关于读书的排比句社区图书漂流公约怎么写关于读书的小报汉书pdf 签的生成,文档的插入等很多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"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln("Heading 3.2"); builder.Writeln("Heading 3.3"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText; // Call the method below to update the TOC. doc.UpdateFields(); doc.Save(@"tttt.doc"); 生成以后把目录文件作为源文件,然后插入文档到目录下: Document mainDoc = new Document("tttt.doc"); Document insertDoc = new Document("insert.doc");//其中insert.doc是要插入的文档// insertDoc.RemoveUnusedResources(); DocumentBuilder builder = new DocumentBuilder(mainDoc); foreach (Bookmark bm in mainDoc.Range.Bookmarks) { if (bm.Name.StartsWith("_Toc")) { 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(@"out.doc"); } 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("The destination node should be either a 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 实现生成目录并且插入文档到目录下】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_531654
暂无简介~
格式:doc
大小:20KB
软件:Word
页数:6
分类:互联网
上传时间:2019-02-28
浏览量:453