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.1
public Node ImportNode(
	Node srcNode,
	bool isImportChildren


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

Return Value

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

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.

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."));

// If we want to add the section from doc2 to doc1, we can't just append them like this:
// dst.AppendChild(src.FirstSection);
// Uncommenting that line throws an exception because doc2's first section belongs to doc2,
// but each node in a document must belong to the document
Assert.AreNotEqual(dst, src.FirstSection.Document);

// We can create a new node that belongs to the destination document
Section importedSection = (Section)dst.ImportNode(src.FirstSection, true);

// It has the same content but it is not the same node nor do they have the same owner
Assert.AreNotEqual(importedSection, src.FirstSection);
Assert.AreNotEqual(importedSection.Document, src.FirstSection.Document);

// Now it is ready to be placed in the document

// Our document does indeed contain both the original and imported section
Assert.AreEqual("Destination document first paragraph text.\r\nSource document first paragraph text.\r\n",
See Also