NodeCollection Class |
Namespace: Aspose.Words
The NodeCollection type exposes the following members.
Name | Description | |
---|---|---|
![]() ![]() | Count |
Gets the number of nodes in the collection.
|
![]() ![]() | Item |
Retrieves a node at the given index.
|
Name | Description | |
---|---|---|
![]() ![]() | Add |
Adds a node to the end of the collection.
|
![]() ![]() | Clear |
Removes all nodes from this collection and from the document.
|
![]() ![]() | Contains |
Determines whether a node is in the collection.
|
![]() | Equals | (Inherited from Object.) |
![]() | GetEnumerator | |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() ![]() | IndexOf |
Returns the zero-based index of the specified node.
|
![]() ![]() | Insert |
Inserts a node into the collection at the specified index.
|
![]() ![]() | Remove |
Removes the node from the collection and from the document.
|
![]() ![]() | RemoveAt |
Removes the node at the specified index from the collection and from the document.
|
![]() ![]() | ToArray |
Copies all nodes from the collection to a new array of nodes.
|
![]() | ToString | (Inherited from Object.) |
NodeCollection does not own the nodes it contains, rather, is just a selection of nodes of the specified type, but the nodes are stored in the tree under their respective parent nodes.
NodeCollection supports indexed access, iteration and provides add and remove methods.
The NodeCollection collection is "live", i.e. changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the NodeCollection properties and methods.
NodeCollection is returned by GetChildNodes(NodeType, Boolean) and also serves as a base class for typed node collections such as SectionCollection, ParagraphCollection etc.
NodeCollection can be "flat" and contain only immediate children of the node it was created from, or it can be "deep" and contain all descendant children.
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); shape.Remove(); } } doc.Save(ArtifactsDir + "Shape.ReplaceTextboxesWithImages.doc");