Rectangle
Contents
[
Hide
]
Create Simple Rectangle
Like previous topics, this one is also about adding a shape and this time the shape we will discuss about is Rectangle. In this topic, we have described that how developers can add simple or formatted rectangles to their slides using Aspose.Slides for .NET . To add a simple rectangle to a selected slide of the presentation, please follow the steps below:
- Create an instance of Presentation class.
- Obtain the reference of a slide by using its Index.
- Add an IAutoShape of Rectangle type using AddAutoShape method exposed by IShapes object.
- Write the modified presentation as a PPTX file.
In the example given below, we have added a simple rectangle to the first slide of the presentation.
// Instantiate Prseetation class that represents the PPTX
using (Presentation pres = new Presentation())
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Add autoshape of rectangle type
sld.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 150, 50);
//Write the PPTX file to disk
pres.Save("RectShp1_out.pptx", SaveFormat.Pptx);
}
Create Formatted Rectangle
To add a formatted rectangle to a slide, please follow the steps below:
- Create an instance of Presentation class.
- Obtain the reference of a slide by using its Index.
- Add an IAutoShape of Rectangle type using AddAutoShape method exposed by IShapes object.
- Set the Fill Type of the Rectangle to Solid.
- Set the Color of the Rectangle using SolidFillColor.Color property as exposed by FillFormat object associated with the IShape object.
- Set the Color of the lines of the Rectangle.
- Set the Width of the lines of the Rectangle.
- Write the modified presentation as PPTX file. The above steps are implemented in the example given below.
// Instantiate Prseetation class that represents the PPTX
using (Presentation pres = new Presentation())
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Add autoshape of rectangle type
IShape shp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 150, 50);
// Apply some formatting to rectangle shape
shp.FillFormat.FillType = FillType.Solid;
shp.FillFormat.SolidFillColor.Color = Color.Chocolate;
// Apply some formatting to the line of rectangle
shp.LineFormat.FillFormat.FillType = FillType.Solid;
shp.LineFormat.FillFormat.SolidFillColor.Color = Color.Black;
shp.LineFormat.Width = 5;
//Write the PPTX file to disk
pres.Save("RectShp2_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}