Summary
This article will introduce how C # manipulates shapes in PPT slides. It mainly involves conventional shapes, such as arrows, rectangles, circles, triangles, polygons, irregular shapes and so on. In the following example, you can draw shapes, format them, and so on. The example contains the following points:
- Draw shape
- Fill in shapes with pictures
- Add text to shapes
- Set Shape Monochrome and Gradient Filling
- Setting Shadow Shadow Effect
- Combine multiple shapes into one
- Setting Shape Edge Effect
- Save the shape as a picture
tool
After downloading and installing, pay attention to adding a reference Spire.Presentation.dll to the program. The dll file can be obtained in the Bin folder under the installation path.
Sample code (for reference)
[Example 1] Drawing shapes
Step 1: Create a new slide
//Create a new slide document and specify the slide size Presentation ppt = new Presentation(); ppt.SlideSize.Type = SlideSizeType.Screen16x9;
Step 2: Get the first slide
ISlide slide = ppt.Slides[0];
Step 3: Add a cloud shape, fill in gradients, and draw text
//Add a cloud shape and fill in the gradient color IAutoShape shape1 = slide.Shapes.AppendShape(ShapeType.CalloutCloud, new RectangleF(160, 50, 200, 80)); shape1.Fill.FillType = FillFormatType.Gradient; shape1.Fill.Gradient.GradientStops.Append(0, Color.Blue); shape1.Fill.Gradient.GradientStops.Append(1, Color.Azure); shape1.Line.FillType = FillFormatType.None; //Draw text in shape and set font, font size, font color, etc. shape1.AppendTextFrame("HOW??"); TextRange textRange = (shape1 as IAutoShape).TextFrame.TextRange; textRange.FontHeight = 13; textRange.LatinFont = new TextFont("Arial"); textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.White;
Step 4: Add ellipse shape and load image filling
IAutoShape shape2 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(50, 130, 150, 250)); string picPath = "sk.png"; shape2.Fill.FillType = FillFormatType.Picture; shape2.Fill.PictureFill.Picture.Url = picPath; shape2.Fill.PictureFill.FillType = PictureFillType.Stretch; shape2.Line.FillType = FillFormatType.None;
Step 5: Add triangles and set border effects, shadow effects
//Add a triangle,Fill in the color and set the border style IAutoShape shape3 = slide.Shapes.AppendShape(ShapeType.Triangle, new RectangleF(480, 180, 100, 130)); shape3.Fill.FillType = FillFormatType.Solid; shape3.Fill.SolidColor.Color = Color.Wheat; shape3.Line.Width = 3; shape3.Line.DashStyle = LineDashStyleType.Dash; shape3.ShapeStyle.LineColor.Color = Color.Red; //Setting Shadow Shadow Effect PresetShadow presetShadow = new PresetShadow(); presetShadow.Preset = PresetShadowValue.BackRightPerspective; presetShadow.ColorFormat.Color = Color.LightGray; shape3.EffectDag.PresetShadowEffect = presetShadow;
Step 6: Add a line with arrows
IAutoShape shape4 = slide.Shapes.AppendShape(ShapeType.Line, new RectangleF(660, 200, 100, 100)); shape4.ShapeStyle.LineColor.Color = Color.Red; shape4.Line.LineEndType = LineEndType.StealthArrow; shape4.Rotation = -90;//Setting Shape Rotation Angle
Step 7: Draw a circle and Pentagon star, set the edge effect, and pull a shape combination.
//Add a circle IAutoShape shape5 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(289, 166, 120, 120)); shape5.Fill.FillType = FillFormatType.Solid; shape5.Fill.SolidColor.Color = Color.White; shape5.Line.FillType = FillFormatType.Solid; shape5.Line.SolidFillColor.Color = Color.Red; //Add a pentagonal star shape IAutoShape shape6 = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(300, 170, 100, 100)); shape6.Fill.FillType = FillFormatType.Solid; shape6.Fill.SolidColor.Color = Color.Orange; shape6.Line.FillType = FillFormatType.None; //The effect of setting the edge of a Pentagon GlowEffect glow = new GlowEffect(); glow.ColorFormat.Color = Color.Yellow; glow.Radius = 7.0; shape6.EffectDag.GlowEffect = glow; //take shape5 and shape6 Combination of two shapes ArrayList list = new ArrayList(); list.Add(shape5); list.Add(shape6); ppt.Slides[0].GroupShapes(list);
Step 8: Save the document
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
After completing the code, debug the running program and generate the document, as shown below.
All code:
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Collections; using System.Drawing; namespace DrawShape_PPT { class Program { static void Main(string[] args) { //Create a new slide document and specify the slide size Presentation ppt = new Presentation(); ppt.SlideSize.Type = SlideSizeType.Screen16x9; //Get the first slide ISlide slide = ppt.Slides[0]; //Add a cloud shape and fill in the gradient color IAutoShape shape1 = slide.Shapes.AppendShape(ShapeType.CalloutCloud, new RectangleF(160, 50, 200, 80)); shape1.Fill.FillType = FillFormatType.Gradient; shape1.Fill.Gradient.GradientStops.Append(0, Color.Blue); shape1.Fill.Gradient.GradientStops.Append(1, Color.Azure); shape1.Line.FillType = FillFormatType.None; //Draw text in shape and set font, font size, font color, etc. shape1.AppendTextFrame("HOW??"); TextRange textRange = (shape1 as IAutoShape).TextFrame.TextRange; textRange.FontHeight = 13; textRange.LatinFont = new TextFont("Arial"); textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.White; //Add an ellipse and fill the shape with an image IAutoShape shape2 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(50, 130, 150, 250)); string picPath = "sk.png"; shape2.Fill.FillType = FillFormatType.Picture; shape2.Fill.PictureFill.Picture.Url = picPath; shape2.Fill.PictureFill.FillType = PictureFillType.Stretch; shape2.Line.FillType = FillFormatType.None; //Add a triangle,Fill in the color and set the shape border style IAutoShape shape3 = slide.Shapes.AppendShape(ShapeType.Triangle, new RectangleF(480, 180, 100, 130)); shape3.Fill.FillType = FillFormatType.Solid; shape3.Fill.SolidColor.Color = Color.Wheat; shape3.Line.Width = 3; shape3.Line.DashStyle = LineDashStyleType.Dash; shape3.ShapeStyle.LineColor.Color = Color.Red; //Set Shadow Shadow Effect PresetShadow presetShadow = new PresetShadow(); presetShadow.Preset = PresetShadowValue.BackRightPerspective; presetShadow.ColorFormat.Color = Color.LightGray; shape3.EffectDag.PresetShadowEffect = presetShadow; //Add a line with arrows IAutoShape shape4 = slide.Shapes.AppendShape(ShapeType.Line, new RectangleF(660, 200, 100, 100)); shape4.ShapeStyle.LineColor.Color = Color.Red; shape4.Line.LineEndType = LineEndType.StealthArrow; shape4.Rotation = -90;//Setting Shape Rotation Angle //Add a circle IAutoShape shape5 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(289, 166, 120, 120)); shape5.Fill.FillType = FillFormatType.Solid; shape5.Fill.SolidColor.Color = Color.White; shape5.Line.FillType = FillFormatType.Solid; shape5.Line.SolidFillColor.Color = Color.Red; //Add a pentagonal star shape IAutoShape shape6 = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(300, 170, 100, 100)); shape6.Fill.FillType = FillFormatType.Solid; shape6.Fill.SolidColor.Color = Color.Orange; shape6.Line.FillType = FillFormatType.None; //The effect of setting the edge of a Pentagon GlowEffect glow = new GlowEffect(); glow.ColorFormat.Color = Color.Yellow; glow.Radius = 7.0; shape6.EffectDag.GlowEffect = glow; //take shape5 and shape6 Combination of two shapes ArrayList list = new ArrayList(); list.Add(shape5); list.Add(shape6); ppt.Slides[0].GroupShapes(list); //Save document ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
[Example 2] Save the shape as a picture
Step 1: Load the test document
Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx");
Step 2: Save the shape as a picture
//Traveling through all the graphics in the first slide for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++) { //Get the graphics in the slide and save them as.png Pictures in format Image image = ppt.Slides[0].Shapes.SaveAsImage(i); image.Save(String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png); }
All code:
using Spire.Presentation; using System; using System.Drawing; namespace SaveShapesAsImgs_PPT { class Program { static void Main(string[] args) { //instantiation Presentation Class objects and load test documents Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx"); //Traveling through all the graphics in the first slide for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++) { //Get the graphics in the slide and save them as.png Pictures in format Image image = ppt.Slides[0].Shapes.SaveAsImage(i); image.Save(String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png); } } } }
(End of this article)
Please indicate the source for reprinting.