CompositeNodeInsertAfter Method

Inserts the specified node immediately after the specified reference node.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0


public Node InsertAfter(
	Node newChild,
	Node refChild


Type: Aspose.WordsNode
The Node to insert.
Type: Aspose.WordsNode
The Node that is the reference node. The newNode is placed after the refNode.

Return Value

Type: Node
The inserted node.


If refChild is null, inserts newChild at the beginning of the list of child nodes.


If the newChild is already in the tree, it is first removed.

If the node being inserted was created from another document, you should use ImportNode(Node, Boolean, ImportFormatMode) to import the node to the current document. The imported node can then be inserted into the current document.


Shows how to replace all textboxes with images.
Document doc = new Document(MyDir + "Textboxes in drawing canvas.docx");

// This gets a live collection of all shape nodes in the document
NodeCollection shapeCollection = doc.GetChildNodes(NodeType.Shape, true);

// Since we will be adding/removing nodes, it is better to copy all collection
// into a fixed size array, otherwise iterator will be invalidated
Node[] shapes = shapeCollection.ToArray();

foreach (Shape shape in shapes.OfType<Shape>())
    // Filter out all shapes that we don't need
    if (shape.ShapeType.Equals(ShapeType.TextBox))
        // Create a new shape that will replace the existing shape
        Shape image = new Shape(doc, ShapeType.Image);

        // Load the image into the new shape
        image.ImageData.SetImage(ImageDir + "Windows MetaFile.wmf");

        // Make new shape's position to match the old shape
        image.Left = shape.Left;
        image.Top = shape.Top;
        image.Width = shape.Width;
        image.Height = shape.Height;
        image.RelativeHorizontalPosition = shape.RelativeHorizontalPosition;
        image.RelativeVerticalPosition = shape.RelativeVerticalPosition;
        image.HorizontalAlignment = shape.HorizontalAlignment;
        image.VerticalAlignment = shape.VerticalAlignment;
        image.WrapType = shape.WrapType;
        image.WrapSide = shape.WrapSide;

        // Insert new shape after the old shape and remove the old shape
        shape.ParentNode.InsertAfter(image, shape);

doc.Save(ArtifactsDir + "Shape.ReplaceTextboxesWithImages.docx");
Shows how to add, update and delete child nodes from a CompositeNode's child collection.
Document doc = new Document();

// An empty document has one paragraph by default
Assert.AreEqual(1, doc.FirstSection.Body.Paragraphs.Count);

// A paragraph is a composite node because it can contain runs, which are another type of node
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Run paragraphText = new Run(doc, "Initial text. ");

// We will place these 3 children into the main text of our paragraph
Run run1 = new Run(doc, "Run 1. ");
Run run2 = new Run(doc, "Run 2. ");
Run run3 = new Run(doc, "Run 3. ");

// We initialized them but not in our paragraph yet
Assert.AreEqual("Initial text.", paragraph.GetText().Trim());

// Insert run2 before initial paragraph text. This will be at the start of the paragraph
paragraph.InsertBefore(run2, paragraphText);

// Insert run3 after initial paragraph text. This will be at the end of the paragraph
paragraph.InsertAfter(run3, paragraphText);

// Insert run1 before every other child node. run2 was the start of the paragraph, now it will be run1

Assert.AreEqual("Run 1. Run 2. Initial text. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(4, paragraph.GetChildNodes(NodeType.Any, true).Count);

// Access the child node collection and update/delete children
((Run)paragraph.GetChildNodes(NodeType.Run, true)[1]).Text = "Updated run 2. ";
paragraph.GetChildNodes(NodeType.Run, true).Remove(paragraphText);

Assert.AreEqual("Run 1. Updated run 2. Run 3.", paragraph.GetText().Trim());
Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);

See Also