Aspose.Words:如何添加另一个WORD文档中的Node对象

简介: 原文:Aspose.Words:如何添加另一个WORD文档中的Node对象首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾: 1 var table = (Table)docSource.
原文: Aspose.Words:如何添加另一个WORD文档中的Node对象

首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾:

1 var table = (Table)docSource.GetChild(NodeType.Table, 0, true);
2 docTarget.FirstSection.Body.ChildNodes.Add(table);

这段代码会抛出异常:“The newChild was created from a different document than the one that created this node.”,这是什么原因呢?

原因是,对于Aspose.Words的Node对象,它的一系列样式和格式的控制,取决于它所在的DocumentBase父对象,这也是很多Aspose.Words对象声明时,必须指定它的DocumentBase参数,比如声明一个Table,应该如下:

1 Document doc=new Document();
2 Table table=new Table(doc);

那么,我们有没有办法添加另一个文档中的对象呢?有,必须通过Document.ImportNode方法或者使用NodeImporter对象。

这两种方法思路都是将源文档中的Node导入到目标文档中,再追加Node到合适的位置。

Document.ImportNode
 1 /// <summary>
 2 /// A manual implementation of the Document.AppendDocument function which shows the general 
 3 /// steps of how a document is appended to another.
 4 /// </summary>
 5 /// <param name="dstDoc">The destination document where to append to.</param>
 6 /// <param name="srcDoc">The source document.</param>
 7 /// <param name="mode">The import mode to use when importing content from another document.</param>
 8 public void AppendDocument(Document dstDoc, Document srcDoc, ImportFormatMode mode)
 9 {
10     // Loop through all sections in the source document. 
11     // Section nodes are immediate children of the Document node so we can just enumerate the Document.
12     foreach (Section srcSection in srcDoc)
13     {
14         // Because we are copying a section from one document to another, 
15         // it is required to import the Section node into the destination document.
16         // This adjusts any document-specific references to styles, lists, etc.
17         //
18         // Importing a node creates a copy of the original node, but the copy
19         // is ready to be inserted into the destination document.
20         Node dstSection = dstDoc.ImportNode(srcSection, true, mode);
21 
22         // Now the new section node can be appended to the destination document.
23         dstDoc.AppendChild(dstSection);
24     }
25 }
NodeImporter
 1 public static Document GenerateDocument(Document srcDoc, ArrayList nodes)
 2         {
 3             // Create a blank document.
 4             Document dstDoc = new Document();
 5             // Remove the first paragraph from the empty document.
 6             dstDoc.FirstSection.Body.RemoveAllChildren();
 7 
 8             // Import each node from the list into the new document. Keep the original formatting of the node.
 9             NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);
10 
11             foreach (Node node in nodes)
12             {
13                 Node importNode = importer.ImportNode(node, true);
14                 dstDoc.FirstSection.Body.AppendChild(importNode);
15             }
16 
17             // Return the generated document.
18             return dstDoc;
19         }

 

参考文档:

http://www.aspose.com/docs/display/wordsnet/Aspose.Words.DocumentBase.ImportNode+Overload_1

目录
相关文章
|
4月前
|
JavaScript 容器
Node.js 全局对象及其使用
Node.js 全局对象及其使用
50 4
|
4月前
【Node学习】—querystring 实现字符串转对象 对象转字符串
【Node学习】—querystring 实现字符串转对象 对象转字符串
|
9月前
vue2 Elementui 树形组件怎么实现多选并获取选中节点的node对象
树形组件是我们经常用到的组件,主要场景就是:公司后台管理的部门管理,做文章目录等。
115 0
|
11月前
|
存储 缓存 JavaScript
Node【Global全局对象】之【Buffer】
Node【Global全局对象】之【Buffer】
78 0
|
11月前
|
移动开发 JavaScript API
Node【Global全局对象】之【URL】
Node【Global全局对象】之【URL】
79 0
|
11月前
|
JavaScript 前端开发 API
Node【Global全局对象】之【Process】
Node【Global全局对象】之【Process】
64 0
|
12月前
|
Web App开发 JavaScript
【Node.js】全局可用变量、函数和对象
在Node.js中提供了一些全局可用的变量、函数和对象,全局就是不需要进行模块加载,可以直接使用的。其中包括全局作用域的函数和对象。也包括不在全局作用域,而在每个模块作用域都存在的变量、函数和对象,在全局可用,但不是golbal对象的属性。
85 0
|
缓存 JavaScript 网络协议
Node.js在Buffers对象在数据报的表现交互在Modules的实战心得
Node.js在Buffers对象在数据报的表现交互在Modules的实战心得
Node.js在Buffers对象在数据报的表现交互在Modules的实战心得
node43-node全局对象global
node43-node全局对象global
76 0
node43-node全局对象global
|
存储 JavaScript 前端开发
Node(5):Buffer 对象的使用
本文简单介绍了 Node.js 中 Buffer 对象的使用,包括创建实例 buffer 的方法,以及操作 buffer 的常用方法。
217 0