ToArray
Contents
[
Hide
]NodeCollection.ToArray method
Copies all nodes from the collection to a new array of nodes.
public Node[] ToArray()
Return Value
An array of nodes.
Remarks
You should not be adding/removing nodes while iterating over a collection of nodes because it invalidates the iterator and requires refreshes for live collections.
To be able to add/remove nodes during iteration, use this method to copy nodes into a fixed-size array and then iterate over the array.
Examples
Shows how to replace all textbox shapes with image shapes.
Document doc = new Document(MyDir + "Textboxes in drawing canvas.docx");
Shape[] shapes = doc.GetChildNodes(NodeType.Shape, true).OfType<Shape>().ToArray();
Assert.AreEqual(3, shapes.Count(s => s.ShapeType == ShapeType.TextBox));
Assert.AreEqual(1, shapes.Count(s => s.ShapeType == ShapeType.Image));
foreach (Shape shape in shapes)
{
if (shape.ShapeType == ShapeType.TextBox)
{
Shape replacementShape = new Shape(doc, ShapeType.Image);
replacementShape.ImageData.SetImage(ImageDir + "Logo.jpg");
replacementShape.Left = shape.Left;
replacementShape.Top = shape.Top;
replacementShape.Width = shape.Width;
replacementShape.Height = shape.Height;
replacementShape.RelativeHorizontalPosition = shape.RelativeHorizontalPosition;
replacementShape.RelativeVerticalPosition = shape.RelativeVerticalPosition;
replacementShape.HorizontalAlignment = shape.HorizontalAlignment;
replacementShape.VerticalAlignment = shape.VerticalAlignment;
replacementShape.WrapType = shape.WrapType;
replacementShape.WrapSide = shape.WrapSide;
shape.ParentNode.InsertAfter(replacementShape, shape);
shape.Remove();
}
}
shapes = doc.GetChildNodes(NodeType.Shape, true).OfType<Shape>().ToArray();
Assert.AreEqual(0, shapes.Count(s => s.ShapeType == ShapeType.TextBox));
Assert.AreEqual(4, shapes.Count(s => s.ShapeType == ShapeType.Image));
doc.Save(ArtifactsDir + "Shape.ReplaceTextboxesWithImages.docx");
See Also
- class Node
- class NodeCollection
- namespace Aspose.Words
- assembly Aspose.Words