CompositeNodeInsertAfter Method |
Namespace: Aspose.Words
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.
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");
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. "); paragraph.AppendChild(paragraphText); // 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. " + (char) 12, paragraph.GetText()); // 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 paragraph.PrependChild(run1); Assert.AreEqual("Run 1. Run 2. Initial text. Run 3. " + (char) 12, paragraph.GetText()); 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. " + (char) 12, paragraph.GetText()); Assert.AreEqual(3, paragraph.GetChildNodes(NodeType.Any, true).Count);