Aspose. Words: how to add a Node object in another word document, aspose. wordsnode

Source: Internet
Author: User

Aspose. Words: how to add a Node object in another word document, aspose. wordsnode

First, let's look at a piece of code. This Code intends to get the first table from docSource and insert it to the end of docTarget:

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

This code throws an exception: "The newChild was created from a different document than the one that created this node.". Why?

The reason is that for Aspose. the Node object of Words. Its Control of a series of styles and formats depends on its DocumentBase parent object, which is also a lot of Aspose. when declaring a Words object, you must specify its DocumentBase parameter. For example, to declare a Table, you should:

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

Is there any way to add objects in another document? Yes. You must use the Document. ImportNode method or NodeImporter object.

The two methods are to import the Node in the source document to the target document, and then append the Node to the appropriate location.

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         }

 

Reference:

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


Does asposewords support online editing of Word documents?

Not understand

How to Use Asposewords to add a header and footer to a Word document (c # net)

Take a look at this link and have the content you need:

Blog.csdn.net/..164947

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.