原文: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. 为了圣像画系统V1.0Beta版(javascript)-GIS520社区

    地图标绘系统V1.0測试版 (界面比較难看,之后再统一美化!)   演示地址:http://www.gis520.com/gis/plotdemo/index.html 查看可在菜单Map--> ...

  2. 如何实现Web聊天

    假设你web聊天不知道这件事情,那么最好的方法可能是:openfire+jsjac openfire它是java做开源xmppserver,jsjac它是javascript做开源的Web版本xmpp ...

  3. SQL Server审计功能入门:CDC(Change Data Capture)

    原文:SQL Server审计功能入门:CDC(Change Data Capture) 介绍 SQL Server 2008引入了CDC(Change Data Capture),它能记录: 1. ...

  4. 有愿意共同发展USB固件做?

    有愿意共同发展USB固件做.现在,它使用STC89S52+PDIUSBD12(环教你玩USB开发板)它实现了一个USB键盘,项目地址:https://github.com/artprogramming ...

  5. Azure File Service in IIS

    微软Azure的Storage套件中提供了新的服务File Service,让我们运行在Azure中的程序都能共享存储,一个存储账号共享的没有上线,但每个共享的上限是5G.由于File Service ...

  6. remine chart2安装

    http://blog.csdn.net/kufeiyun/article/details/9213911

  7. 如何解决KEIL 5 编KEIL4同RTX系统的project解

    1.我个人KEIL5与KEIL4对照 相较于KEIL 5 的"华丽".笔者还是喜欢KEIL4的"内敛",主要也还是习惯了.懒得换了.由于工作的  原      ...

  8. 挑逗B少年搞计划10 假设你是愿意用我的心脏层剥离一层~

        这些天都非常推迟考试啊.然后,学校已安排一周培训,是的.在延迟学习,大狼医院我真的是正常水平.     幸好我们周六周日不让放假了,不然预计进度直接就停了.这两天也是抽出了时间把敲了一下三层的 ...

  9. 关于通过id查询记录的一些总结

    最近在做一个oa系统,简化了账号的设置,列名均为id,类型均为varchar:有的表将id设置成了主键,有的表没有设置成主键. 通过举例说明通过id查询的一些问题. 之前登陆的时候,账号001-007 ...

  10. Swift游戏开发实战教程(霸内部信息大学)

    Swift游戏开发实战教程(大学霸内部资料) 试读下载地址:http://pan.baidu.com/s/1sj7DvQH 介绍:本教程是国内第一本Swift游戏开发专向资料. 本教程具体解说记忆配对 ...