Represents an object in the drawing layer, such as an AutoShape, textbox, freeform, OLE object, ActiveX control, or picture.
For a list of all members of this type, see Shape Members.
System.Object
Aspose.Words.Node
Aspose.Words.CompositeNode
Aspose.Words.Drawing.ShapeBase
Aspose.Words.Drawing.Shape
[Visual Basic]Public Class Shape
Remarks
Using the Shape class you can create or modify shapes in a Microsoft Word document.
An important property of a shape is its ShapeType. Shapes of different types can have different capabilities in a Word document. For example, only image and OLE shapes can have images inside them. Most of the shapes can have text, but not all.
Shapes that can have text, can contain Paragraph and Table nodes as children.
Example
Shows how to delete all shapes from a node.
[C#]
// Here we get all shapes from the document node, but you can do this for any smaller
// node too, for example delete shapes from a single section or a paragraph.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);
shapes.Clear();
// There could also be group shapes, they have different node type, remove them all too.
NodeCollection groupShapes = doc.GetChildNodes(NodeType.GroupShape, true, false);
groupShapes.Clear();
[Visual Basic]
' Here we get all shapes from the document node, but you can do this for any smaller
' node too, for example delete shapes from a single section or a paragraph.
Dim shapes As NodeCollection = doc.GetChildNodes(NodeType.Shape, True, False)
shapes.Clear()
' There could also be group shapes, they have different node type, remove them all too.
Dim groupShapes As NodeCollection = doc.GetChildNodes(NodeType.GroupShape, True, False)
groupShapes.Clear()
Shows how to extract images from a document and save them as files.
[C#]
[Test]
public void ExtractImagesToFiles()
{
Document doc = new Document(MyDir + "Image.SampleImages.doc");
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);
int imageIndex = 0;
foreach (Shape shape in shapes)
{
if (shape.HasImage)
{
string imageFileName = string.Format("Image.ExportImages.{0} Out.{1}", imageIndex, shape.ImageData.ImageType);
shape.ImageData.Save(MyDir + imageFileName);
imageIndex++;
}
}
}[Visual Basic]
<Test> _
Public Sub ExtractImagesToFiles()
Dim doc As Document = New Document(MyDir & "Image.SampleImages.doc")
Dim shapes As NodeCollection = doc.GetChildNodes(NodeType.Shape, True, False)
Dim imageIndex As Integer = 0
For Each shape As Shape In shapes
If shape.HasImage Then
Dim imageFileName As String = String.Format("Image.ExportImages.{0} Out.{1}", imageIndex, shape.ImageData.ImageType)
shape.ImageData.Save(MyDir & imageFileName)
imageIndex += 1
End If
Next shape
End SubShows how to insert a floating image in the middle of a page.
[C#]
// This creates a builder and also an empty document inside the builder.
DocumentBuilder builder = new DocumentBuilder();
// By default, the image is inline.
Shape shape = builder.InsertImage(MyDir + "Aspose.Words.gif");
// Make the image float, put it behind text and center on the page.
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.VerticalAlignment = VerticalAlignment.Center;
builder.Document.Save(MyDir + "Image.CreateFloatingPageCenter Out.doc");
[Visual Basic]
' This creates a builder and also an empty document inside the builder.
Dim builder As DocumentBuilder = New DocumentBuilder()
' By default, the image is inline.
Dim shape As Shape = builder.InsertImage(MyDir & "Aspose.Words.gif")
' Make the image float, put it behind text and center on the page.
shape.WrapType = WrapType.None
shape.BehindText = True
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
shape.HorizontalAlignment = HorizontalAlignment.Center
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page
shape.VerticalAlignment = VerticalAlignment.Center
builder.Document.Save(MyDir & "Image.CreateFloatingPageCenter Out.doc")
Shows how to delete all images from a document.
[C#]
// Here we get all shapes from the document node, but you can do this for any smaller
// node too, for example delete shapes from a single section or a paragraph.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);
// We cannot delete shape nodes while we enumerate through the collection.
// One solution is to add nodes that we want to delete to a temporary array and delete afterwards.
ArrayList shapesToDelete = new ArrayList();
foreach (Shape shape in shapes)
{
// Several shape types can have an image including image shapes and OLE objects.
if (shape.CanHaveImage)
shapesToDelete.Add(shape);
}
// Now we can delete shapes.
foreach (Shape shape in shapesToDelete)
shape.Remove();[Visual Basic]
' Here we get all shapes from the document node, but you can do this for any smaller
' node too, for example delete shapes from a single section or a paragraph.
Dim shapes As NodeCollection = doc.GetChildNodes(NodeType.Shape, True, False)
' We cannot delete shape nodes while we enumerate through the collection.
' One solution is to add nodes that we want to delete to a temporary array and delete afterwards.
Dim shapesToDelete As ArrayList = New ArrayList()
For Each shape As Shape In shapes
' Several shape types can have an image including image shapes and OLE objects.
If shape.CanHaveImage Then
shapesToDelete.Add(shape)
End If
Next shape
' Now we can delete shapes.
For Each shape As Shape In shapesToDelete
shape.Remove()
Next shapeOpens an HTML document with images from a stream with a base URI.
[C#]
// We are opening this HTML file:
/*
<html>
<body>
<p>Simple file.</p>
<p><img src="Aspose.Words.gif" width="80" height="60"></p>
</body>
</html>
*/
string fileName = MyDir + "Document.OpenFromStreamWithBaseUri.html";
// Open the stream.
Stream stream = File.OpenRead(fileName);
// Open the document. Note the Document constructor detects HTML format automatically.
// Pass the URI of the base folder so any images with relative URIs in the file can be found.
Document doc = new Document(stream, MyDir);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();
// Lets make sure the image was imported successfully into a Shape node.
// Get the 1st shape node in the document.
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
// Verify some properties of the image.
Assert.IsTrue(shape.IsImage);
Assert.IsNotNull(shape.ImageData.ImageBytes);
Assert.AreEqual(80.0, ConvertUtil.PointToPixel(shape.Width));
Assert.AreEqual(60.0, ConvertUtil.PointToPixel(shape.Height));
// Save in the DOC format.
doc.Save(MyDir + "Document.OpenFromStreamWithBaseUri Out.doc");[Visual Basic]
' We are opening this HTML file:
'
'<html>
'<body>
'<p>Simple file.</p>
'<p><img src="Aspose.Words.gif" width="80" height="60"></p>
'</body>
'</html>
'
Dim fileName As String = MyDir & "Document.OpenFromStreamWithBaseUri.html"
' Open the stream.
Dim stream As Stream = File.OpenRead(fileName)
' Open the document. Note the Document constructor detects HTML format automatically.
' Pass the URI of the base folder so any images with relative URIs in the file can be found.
Dim doc As Document = New Document(stream, MyDir)
' You can close the stream now, it is no longer needed because the document is in memory.
stream.Close()
' Lets make sure the image was imported successfully into a Shape node.
' Get the 1st shape node in the document.
Dim shape As Shape = CType(doc.GetChild(NodeType.Shape, 0, True), Shape)
' Verify some properties of the image.
Assert.IsTrue(shape.IsImage)
Assert.IsNotNull(shape.ImageData.ImageBytes)
Assert.AreEqual(80.0, ConvertUtil.PointToPixel(shape.Width))
Assert.AreEqual(60.0, ConvertUtil.PointToPixel(shape.Height))
' Save in the DOC format.
doc.Save(MyDir & "Document.OpenFromStreamWithBaseUri Out.doc")Requirements
Namespace: Aspose.Words.Drawing
Assembly: Aspose.Words (in Aspose.Words.dll)
See Also
Shape Members | Aspose.Words.Drawing Namespace | ShapeBase | GroupShape