Problem getting/deleting shapes

I’ve created a shape in my document.

Using this simple code I expect my output to be a document without that shape.

Using inStream As New MemoryStream(My.Resources.DocumentWithShape)

Dim loadDoc As New Aspose.Words.Document(inStream)

For Each shape As Aspose.Words.Drawing.Shape In loadDoc.GetChildNodes(Aspose.Words.NodeType.Shape, True)

shape.Remove()

Next

loadDoc.Save "c:\DocumentStillContainsShape.docx")

End Using

Console.WriteLine("Documents saved")

Console.ReadLine()

Yet for some reason the saved document still contains the shape.

When debugging I noticed “loadDoc.GetChildNodes(Aspose.Words.NodeType.Shape, True)” cannot find the shape that is in the document.

Is this a bug or am I doing something wrong?

Thanks in advance!

Kris

Hi Kris,

Thanks for your inquiry. Your document contains DrawingML node not Shape node. Please use the following code snippet to achieve you requirements.

For Each drawingML As Aspose.Words.Drawing.Shape In loadDoc.GetChildNodes(Aspose.Words.NodeType.DrawingML, True)
drawingML.Remove()
Next

DrawingML class represents a DrawingML shape, picture, chart or diagram in a document. Shape Class represents an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture.

Please let us know if you have any more queries.

Thanks for the quick reply!

The remove worked like this.

Yet, i found a simular scenario and I managed to reproduce this problem if I use a textbox.

As you said a textbox falls under the ‘shape’ category but the code:

“loadDoc.GetChildNodes(Aspose.Words.NodeType.Shape, True)” does not return anything. Even though there are 2 textboxes inside this document.

Thanks in advance!

Kind regards,

Kris

Hi Kris,

Thanks for your inquiry. The textbox in Docx is represented by DrawingML and in Doc file format it is Shape. Please save the document to Doc file format and use the same code to get the Shape nodes. Hope this answers your query. Please let us know if you have any more queries.