Free Support Forum -

Inserting images while using the drawing canvas

I am having trouble inserting images when there is a drawing canvas on a word document. Basically, I have this program that will look for the location of a text box and replace the text box with an image. When the text box is in a drawing canvas I get the following error: The node must be a paragraph or a direct child of a paragraph. For free
technical support, please post this error and the file in the Aspose.Words

When the text box is not in the drawing canvas my code works fine. Is there anything special that I have to do to access text boxes in a drawing canvas?

Here is my code (VB.NET):


Dim x As Integer
Dim objNC As Aspose.Words.NodeCollection
Dim objNode As Aspose.Words.Node
Dim objShape As Aspose.Words.Drawing.Shape
Dim y As Integer

Dim objdoc As New Aspose.Words.Document(“C:\temp\test.doc”)

Dim objDB As Aspose.Words.DocumentBuilder = New Aspose.Words.DocumentBuilder(objdoc)

objNC = objDB.Document.GetChildNodes(Aspose.Words.NodeType.Shape, True)

x = objNC.Count 'keep track of number of shapes

For y = 0 To x 'checks each node since removing a text box node exits the objNode loop

For Each objNode In objNC

'set the shape equal to the node
objShape = objNode

objShape = objDB.InsertImage(System.Drawing.Image.FromFile(“c:\Temp\logo.jpg”), objShape.RelativeHorizontalPosition, objShape.Left, objShape.RelativeVerticalPosition, objShape.Top, objShape.Width, objShape.Height, objShape.WrapType.Square)
Catch ex As Exception
MsgBox("Invalid image " & ex.Message)
Exit For
End Try




'save the file

Catch ex As Exception
MsgBox("Unable to add image " & ex.Message)
End Try

DocumentBuilder.MoveTo can move cursor only into a paragraph. In your case you end up moving the cursor into a drawing canvas (group shape), this is not allowed.

You can do this without DocumentBuilder:

Document doc = new Document(MyPath + "Shape.ReplaceTextboxesWithImages.doc");

// This gets a live collection of all shape nodes in the document.

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Loop through the collection using index.

// (foreach enumeration will not work since we modify the collection of shapes).

for (int i = 0; i < shapes.Count; i++)


// The collection is live and we replace exactly one shape with exactly

// one other shape, so loop by an index that increments by 1 will work fine.

Shape shape = (Shape)shapesIdea [I];

// Filter out all shapes that we don't need.

if (shape.ShapeType == 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(MyPath + "Hammer.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(MyPath + "Shape.ReplaceTextboxesWithImages Out.doc");