How to add shapes to slides in C#?

Shape is widely used on the presentation slides to make it more vivid and beautiful. Spire.Presentation for .NET supports adding different kinds of shapes to the slides, such as triangle, rectangle, ellipse, star, line and so on. In this topic, we will show you how to add shapes and formatting these shapes to the slides in C#.

Here comes to the code snippet:

Step 1: Create a PPT document.

Presentation presentation = new Presentation();

Step 2: Add shapes to the slides.

// append new shape - Triangle
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Triangle, new RectangleF(50, 100, 100, 100));

//append new shape - Rectangle
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(225, 100, 150, 100));

//append new shape - Ellipse
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(450, 100, 150, 100));

//append new shape - FivePointedStar
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(50, 250, 100, 100));

//append new shape - Line
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Line, new RectangleF(300, 250, 160, 120));

Step 3: Apply some formatting on the shape.

//set the color and fill style of shape
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.LightGreen;
shape.ShapeStyle.LineColor.Color = Color.White;

Step 4: Save and launch to view the PPTX document.

presentation.SaveToFile("shape.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("shape.pptx");

Effective Screenshot:

How to add shapes to slides in C#?

Full codes:

//create PPT document
Presentation presentation = new Presentation();

//append new shape - Triangle
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Triangle, new RectangleF(50, 100, 100, 100));

//set the color and fill style of shape
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.LightGreen;
shape.ShapeStyle.LineColor.Color = Color.White;

//append new shape - Rectangle
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(225, 100, 150, 100));

//append new shape - Ellipse
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(450, 100, 150, 100));
            
//append new shape - FivePointedStar
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(50, 250, 100, 100));


//append new shape - Line
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Line, new RectangleF(300, 250, 260, 260));
shape.Rotation = -45;
   
           
//save the document
presentation.SaveToFile("shape.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("shape.pptx");