Aspose.words: How to add a Node object in another Word document

Source: Internet
Author: User

First look at a piece of code that is intended to fetch the first table from Docsource and insert the end of the Doctarget:

1 var 0 true ); 2 docTarget.FirstSection.Body.ChildNodes.Add (table);

This code throws an exception: "The NewChild is created from a different document than the one, and the created this node."

The reason is that For a Aspose.words node object, its control over a series of styles and formatting, depending on the Documentbase parent object it is in, which is also a lot of aspose.words object declarations, you must specify its documentbase parameters, such as declaring a table, as follows:

1 document doc=new  document (); 2 table table=new table (DOC);

So, do we have a way to add objects from another document? There, you must either pass the Document.importnode method or use the Nodeimporter object.

Both of these approaches are to import node from the source document into the target document, and then append node to the appropriate location.

Document.importnode
1 /// <summary>2 ///A Manual implementation of the Document.appenddocument function which shows the general3 ///steps of how a document was appended to another.4 /// </summary>5 /// <param name= "Dstdoc" >The destination document where to append.</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 voidappenddocument (document Dstdoc, document SRCDOC, Importformatmode mode)9 {Ten     //Loop through all sections in the source document. One     //Section nodes is immediate children of the document node so we can just enumerate the document. A     foreach(Section SrcsectioninchSrcdoc) -     { -         //Because We are copying a sections from one document to another, the         //It's required to import the sections 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); +  A         //Now, the new section node can is appended to the destination document. at Dstdoc.appendchild (dstsection); -     } -}
Nodeimporter
1  Public StaticDocument Generatedocument (document SRCDOC, ArrayList nodes)2         {3             //Create a blank document.4Document Dstdoc =NewDocument ();5             //Remove the first paragraph from the empty document.6 DstDoc.FirstSection.Body.RemoveAllChildren ();7 8             //Import each node from the list to the new document. Keep the original formatting of the node.9Nodeimporter Importer =NewNodeimporter (Srcdoc, Dstdoc, importformatmode.keepsourceformatting);Ten  One             foreach(Node nodeinchnodes) A             { -Node ImportNode = importer. ImportNode (node,true); - DstDoc.FirstSection.Body.AppendChild (importnode); the             } -  -             //Return the generated document. -             returnDstdoc; +}

Reference Documentation:

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

Aspose.words: How to add a Node object in another Word document

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.