DocumentBaseImportNode Method (Node, Boolean)

Imports a node from another document to the current document.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.5
Syntax
public Node ImportNode(
	Node srcNode,
	bool isImportChildren
)

Parameters

srcNode
Type: Aspose.WordsNode
The node being imported.
isImportChildren
Type: SystemBoolean
True to import all child nodes recursively; otherwise, false.

Return Value

Type: Node
The cloned node that belongs to the current document.
Remarks

This method uses the UseDestinationStyles option to resolve formatting.

Importing a node creates a copy of the source node belonging to the importing document. The returned node has no parent. The source node is not altered or removed from the original document.

Before a node from another document can be inserted into this document, it must be imported. During import, document-specific properties such as references to styles and lists are translated from the original to the importing document. After the node was imported, it can be inserted into the appropriate place in the document using InsertBefore(Node, Node) or InsertAfter(Node, Node).

If the source node already belongs to the destination document, then simply a deep clone of the source node is created.

Examples
Shows how to import node from source document to destination document.
Document src = new Document();
Document dst = new Document();

// Add text to both documents
src.FirstSection.Body.FirstParagraph.AppendChild(new Run(src, "Source document first paragraph text."));
dst.FirstSection.Body.FirstParagraph.AppendChild(new Run(dst, "Destination document first paragraph text."));

// In order for a child node to be successfully appended to another node in a document,
// both nodes must have the same parent document, or an exception is thrown
Assert.AreNotEqual(dst, src.FirstSection.Document);
Assert.Throws<ArgumentException>(() => { dst.AppendChild(src.FirstSection); });

// For that reason, we can't just append a section of the source document to the destination document using Node.AppendChild()
// Document.ImportNode() lets us get around this by creating a clone of a node and sets its parent to the calling document
Section importedSection = (Section)dst.ImportNode(src.FirstSection, true);

// Now it is ready to be placed in the document
dst.AppendChild(importedSection);

// Our document now contains both the original and imported section
Assert.AreEqual("Destination document first paragraph text.\r\nSource document first paragraph text.\r\n",
    dst.ToString(SaveFormat.Text));
See Also