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

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

 var table = (Table)docSource.GetChild(NodeType.Table, , 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对象的更多相关文章

  1. 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)

    Sub 自动调整所有表格() ' ' 自动调整所有表格 宏 ' 'Application.Browser.Target = wdBrowseTable For i = 1 To ActiveDocum ...

  2. 【转】WPS word 文档中的插入对象 为什么打不开

    点击桌面左下角开始按钮--所有程序,找到wps office文件夹--wps office工具--配置工具--高级--兼容设置,否选兼容第三方软件.

  3. Word文档中的语法高亮显示代码

    有时候我们程序员也需要在word文档里面显示代码,但是直接复制过去 不好看,格式也不太对,这里给大家分享一个Word文档中的语法高亮显示代码的方法 http://www.planetb.ca/synt ...

  4. C# 复制一个Word文档的部分或全部内容到另一个Word文档

    C# 复制一个Word文档的部分或全部内容到另一个Word文档 我最近喜欢折腾Office软件相关的东西,想把很多Office软件提供的功能用.NET来实现,如果后期能把它用来开发一点我自己的小应用程 ...

  5. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  6. aspose.words复制插入同一word文档中的某个页面

    选择word模板 Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name. ...

  7. 如何在word文档中添加mathtype加载项

    MathType是强大的数学公式编辑器,通常与office一起使用,mathtype安装完成后,正常情况下会在word文档中的菜单中自动添加mathtype加载项,但有时也会出现小意外,mathtyp ...

  8. 向Docx4j生成的word文档中添加布局--第二部分

    原文标题:Adding layout to your Docx4j-generated word documents, part 2 原文链接:http://blog.iprofs.nl/2012/1 ...

  9. C# 在Word文档中生成条形码

    C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...

随机推荐

  1. c#-RTF文本编辑器

    1".RTF"什么? 多信息文本格式 (RTF) 是一种方便于不同的设备.系统查看的文本和图形文档格式. RTF 使用美国国内标准协会 (ANSI). PC-8. Macintos ...

  2. IT该忍者神龟Jquery小工具easyUI物业摘要召回

    找了个时间看了下EasyUI插件.对它的插件感觉是非常舒服,特地把Easy UI的大部分功能属性做了一下汇总. 此属性列表请对比jQuery EasyUI 1.0.5,关于它的很多其它资讯请猛击这里. ...

  3. SQL开发中容易忽视的一些小地方(二)

    原文:SQL开发中容易忽视的一些小地方(二) 目的:继上一篇:SQL开发中容易忽视的一些小地方(一) 总结SQL中的null用法后,本文我将说说表联接查询. 为了说明问题,我创建了两个表,分别是学生信 ...

  4. Balanced Binary Tree(Java代码没有结束,是什么原因???)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. Java的结构之美【1】——构造对象

    当我们遇到多个构造器參数的时候可能会想到用构件器,代码例如以下: /** * 构建器 * @author 阳光小强 * */ public class Lunch { private String c ...

  6. Meteor全栈开发平台

    Meteor全栈开发平台 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonno ...

  7. 批处理删除IIS的everyone、users的访问权限

    原文 批处理删除IIS的everyone.users的访问权限 以下批处理代码功能,实现的是,删除C盘的everyone.users用户对IIS的权限. 一.删除C盘的everyone的权限 cd/  ...

  8. ASP.NET AJAX简明教程

     当我们谈论Ajax时,首先想到的就是JavaScript下的Ajax,用来完成网页的交互,局部刷新工作,Microsoft的ASP.NET AJAX框架在Web的开发中承担着类似的角色,并简化了Ja ...

  9. BroadcastReceiver.PendingResult类别

    java.lang.Object android.content.BroadcastReceiver.PendingResul 类概述 状态的结果正在等待一个广播接收器.在BroadcastRecei ...

  10. 【iOS开展-94】xcode6如何使用GIT以及如何添加太老项目GIT特征?

    (1)对于一个新项目:如何使用GIT?在新项目的过程,例如,您可以选择下面的复选框. (2)针对老项目,加入GIT功能. --在终端.cd到项目文件夹 --然后输入git init,初始化一个.git ...