Aspose Word 实现生成目录并且插入文档到目录下
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; } } }
…… 此处隐藏:1220字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [政务民生]2013年公共基础知识热点问题(七)
- [政务民生]检验检测机构资质认定评审准则及释义20
- [政务民生]关于印发重庆市房屋建筑和市政基础设施
- [政务民生]1、隧道洞身开挖支护施工技术交底书
- [政务民生]2015年山东省17地市中考语文试题分类汇
- [政务民生]2-高级会计师资格考试和评审流程图
- [政务民生]2018版中国清分机行业发展分析及前景策
- [政务民生]新课改高中政治探究
- [政务民生]2018-2024年中国新型组合房屋行业投资
- [政务民生]2015年上海市春季高考数学模拟试卷五
- [政务民生]灌砂法及环刀法测压实度(带计算过程)
- [政务民生]运筹学实验2求解非线性规划
- [政务民生]劝学、逍遥游默写(教师卷)
- [政务民生]《运筹学》 - 期末考试 - 试卷A - 答案
- [政务民生]八年级英语下册 Module 6 Hobbies测试
- [政务民生]2019年宪法知识竞赛试题库100题(含答
- [政务民生]自动化英文文献翻译
- [政务民生]公文格式实施细则
- [政务民生]高一地理上册课堂跟踪练习题6
- [政务民生]会计继续教育习题及答案
- 第三章 无约束最优化方法
- 泛读教程第三册答案
- 魏晋南北朝文学
- 幂的运算复习题
- 城市环境问题的成因与治理策略_以社会
- 钢结构行业产业链及竞争分析研究
- 新型热塑性弹性体增韧聚丙烯的研究
- 中国旅游地理B卷试题及答案
- (苏教版)五年级数学上册第三单元测试卷
- 不稳定性心绞痛诊断与治疗
- 俞氏国际后勤职能部门绩效考核办法
- GB7258-2017新标准考试题含答案
- 小学生汉字听写比赛活动方案
- 1.3《平抛运动》学案 教科版必修2
- 2011香港特别行政区公务员考试复习资料
- 考虑水力条件变化的城市给水管网可靠性
- 表面活性剂在油田开发和生产中的应用
- ITT内部培训资料-FI端吸泵的介绍
- 文明守纪,从我做起学生发言稿
- 初中读《聊斋志异》心得体会800字范文




