Aspose.Words:如何添加另一个WORD文档中的Node对象
2014-09-04 08:56
531 查看
首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾:
这段代码会抛出异常:“The newChild was created from a different document than the one that created this node.”,这是什么原因呢?
原因是,对于Aspose.Words的Node对象,它的一系列样式和格式的控制,取决于它所在的DocumentBase父对象,这也是很多Aspose.Words对象声明时,必须指定它的DocumentBase参数,比如声明一个Table,应该如下:
那么,我们有没有办法添加另一个文档中的对象呢?有,必须通过Document.ImportNode方法或者使用NodeImporter对象。
这两种方法思路都是将源文档中的Node导入到目标文档中,再追加Node到合适的位置。
Document.ImportNode
NodeImporter
参考文档:
http://www.aspose.com/docs/display/wordsnet/Aspose.Words.DocumentBase.ImportNode+Overload_1
var table = (Table)docSource.GetChild(NodeType.Table, 0, true); 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,应该如下:
Document doc=new Document(); Table table=new Table(doc);
那么,我们有没有办法添加另一个文档中的对象呢?有,必须通过Document.ImportNode方法或者使用NodeImporter对象。
这两种方法思路都是将源文档中的Node导入到目标文档中,再追加Node到合适的位置。
Document.ImportNode
/// <summary> /// A manual implementation of the Document.AppendDocument function which shows the general /// steps of how a document is appended to another. /// </summary> /// <param name="dstDoc">The destination document where to append to.</param> /// <param name="srcDoc">The source document.</param> /// <param name="mode">The import mode to use when importing content from another document.</param> public void AppendDocument(Document dstDoc, Document srcDoc, ImportFormatMode mode) { // Loop through all sections in the source document. // Section nodes are immediate children of the Document node so we can just enumerate the Document. foreach (Section srcSection in srcDoc) { // Because we are copying a section from one document to another, // it is required to import the Section node into the destination document. // This adjusts any document-specific references to styles, lists, etc. // // Importing a node creates a copy of the original node, but the copy // is ready to be inserted into the destination document. Node dstSection = dstDoc.ImportNode(srcSection, true, mode); // Now the new section node can be appended to the destination document. dstDoc.AppendChild(dstSection); } }
NodeImporter
public static Document GenerateDocument(Document srcDoc, ArrayList nodes) { // Create a blank document. Document dstDoc = new Document(); // Remove the first paragraph from the empty document. dstDoc.FirstSection.Body.RemoveAllChildren(); // Import each node from the list into the new document. Keep the original formatting of the node. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting); foreach (Node node in nodes) { Node importNode = importer.ImportNode(node, true); dstDoc.FirstSection.Body.AppendChild(importNode); } // Return the generated document. return dstDoc; }
参考文档:
http://www.aspose.com/docs/display/wordsnet/Aspose.Words.DocumentBase.ImportNode+Overload_1
相关文章推荐
- Aspose.Words:如何添加另一个WORD文档中的Node对象
- Aspose.Words:如何添加另一个WORD文档中的Node对象
- Aspose.Words:如何添加另一个WORD文档中的Node对象
- Aspose.Words:如何添加另一个WORD文档中的Node对象
- Aspose.Words如何在文档中添加水印
- Aspose.Words在word文档合并时如何控制列表编号
- 在WORD文档里如何添加编号
- 用Aspose.Words 从Word文档中提取表格数据
- linq 把 xml的值 赋值给其他? linq to xml 如何赋值? xml里面的特殊符号转义,asp.net word域合并 Aspose.Words 在线生成word
- 在word文档中插入外部对象(例如插入另一个外部word文档或excel文档)
- 无需安装Microsoft Words即可读写打印Word文档的控件Aspose.Words
- Aspose.Words for .NET动态生成word文档中的图片或水印
- Aspose.Words .NET如何实现文档合并的同页分页显示
- asp.net下用Aspose.Words for .NET动态生成word文档中的数据表格的方法
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法
- 用Aspose.Words 从Word文档中提取表格数据
- 如何用Aspose.Words.dll把World文档生成图片
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法(转)
- 使用Aspose.BarCode 和 Aspose.Words从Word文档中识别条码
- 【转载自某园友】用Aspose.Words for .NET动态生成word文档中的数据表格