DocumentBaseImportNode Method (Node, Boolean, ImportFormatMode)

Imports a node from another document to the current document with an option to control formatting.

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.9
public Node ImportNode(
	Node srcNode,
	bool isImportChildren,
	ImportFormatMode importFormatMode


Type: Aspose.WordsNode
The node to imported.
Type: SystemBoolean
True to import all child nodes recursively; otherwise, false.
Type: Aspose.WordsImportFormatMode
Specifies how to merge style formatting that clashes.

Return Value

Type: Node
The cloned, imported node. The node belongs to the destination document, but has no parent.

This overload is useful to control how styles and list formatting are imported.

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 with specific options.

// Create two documents with two styles that aren't the same but have the same name
Document src = new Document();
Style srcStyle = src.Styles.Add(StyleType.Character, "My style");
DocumentBuilder srcBuilder = new DocumentBuilder(src);
srcBuilder.Font.Style = srcStyle;
srcBuilder.Writeln("Source document text.");

Document dst = new Document();
Style dstStyle = dst.Styles.Add(StyleType.Character, "My style");
dstStyle.Font.Bold = true;
DocumentBuilder dstBuilder = new DocumentBuilder(dst);
dstBuilder.Font.Style = dstStyle;
srcBuilder.Writeln("Destination document text.");

dst.ImportNode(src.FirstSection, true, ImportFormatMode.UseDestinationStyles);

Assert.IsNull(dst.Styles["My style_0"]);

dst.ImportNode(src.FirstSection, true, ImportFormatMode.KeepDifferentStyles);

Assert.IsNotNull(dst.Styles["My style_0"]);
See Also