How to Add Shapes to PowerPoint in .NET
Aspose.Slides FOSS for .NET supports adding AutoShapes, Tables, Connectors, and PictureFrames to presentation slides. All shape types are added through the slide.Shapes collection.
Step-by-Step Guide
Step 1: Install the Package
dotnet add package Aspose.Slides.FossVerify the installation:
using Aspose.Slides.Foss;
Console.WriteLine("Ready");Step 2: Create a Presentation
Always use Presentation with a using statement.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// ... add shapes ...
prs.Save("output.pptx", SaveFormat.Pptx);Step 3: Add an AutoShape
slide.Shapes.AddAutoShape(shapeType, x, y, width, height) places a shape at the given position and size (all in points). Use ShapeType constants to select the shape.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// Rectangle
var rect = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 300, 100);
rect.AddTextFrame("Rectangle shape");
// Ellipse
var ellipse = slide.Shapes.AddAutoShape(ShapeType.Ellipse, 400, 50, 200, 100);
ellipse.AddTextFrame("Ellipse shape");
prs.Save("autoshapes.pptx", SaveFormat.Pptx);Step 4: Add a Table
slide.Shapes.AddTable(x, y, columnWidths, rowHeights) creates a table at the specified position. Column widths and row heights are arrays of point values.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var colWidths = new double[] { 150.0, 150.0, 150.0 };
var rowHeights = new double[] { 40.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 200, colWidths, rowHeights);
// Set header row text
string[] headers = { "Product", "Units", "Revenue" };
for (int col = 0; col < headers.Length; col++)
table.Rows[0][col].TextFrame.Text = headers[col];
// Set data rows
string[][] rows = {
new[] { "Widget A", "120", "$2,400" },
new[] { "Widget B", "85", "$1,700" },
};
for (int rowIdx = 0; rowIdx < rows.Length; rowIdx++)
for (int col = 0; col < rows[rowIdx].Length; col++)
table.Rows[rowIdx + 1][col].TextFrame.Text = rows[rowIdx][col];
prs.Save("table.pptx", SaveFormat.Pptx);Step 5: Add a Connector
Connectors link two shapes visually. Create the shapes first, then add a connector and set its start and end connection points.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var box1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 100, 150, 60);
box1.AddTextFrame("Start");
var box2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 350, 100, 150, 60);
box2.AddTextFrame("End");
var conn = slide.Shapes.AddConnector(ShapeType.BentConnector3, 0, 0, 10, 10);
conn.StartShapeConnectedTo = box1;
conn.StartShapeConnectionSiteIndex = 3; // right side of box1
conn.EndShapeConnectedTo = box2;
conn.EndShapeConnectionSiteIndex = 1; // left side of box2
prs.Save("connector.pptx", SaveFormat.Pptx);Connection site indices are numbered 0-3 for a rectangle: top=0, left=1, bottom=2, right=3.
Step 6: Add a Picture Frame
Embed an image and add it to the slide as a PictureFrame. Read the image bytes first, add them to the presentation’s image collection, then create the frame.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
byte[] imageData = File.ReadAllBytes("logo.png");
var image = prs.Images.AddImage(imageData);
var slide = prs.Slides[0];
slide.Shapes.AddPictureFrame(
ShapeType.Rectangle, // bounding shape type
50, 50, // x, y in points
200, 150, // width, height in points
image
);
prs.Save("with-image.pptx", SaveFormat.Pptx);Common Issues and Fixes
Shape appears outside the visible slide area
Slides are 720 x 540 points by default. Values of x or y beyond those bounds place the shape off-slide. Keep x < 720 and y < 540, and ensure x + width <= 720 and y + height <= 540.
NullReferenceException when accessing TextFrame
AddAutoShape() returns the shape object directly. If you see null, check that you are not discarding the return value.
Table cell text is empty after assignment
The correct property is .TextFrame.Text (not .Text directly on the cell). Access cells as table.Rows[rowIndex][colIndex].TextFrame.Text = "value".
Frequently Asked Questions
How many shapes can I add to a slide?
There is no library-imposed limit. Practical limits depend on file size and the rendering capability of your target PPTX viewer.
Can I change a shape’s position after adding it?
Yes. The shape object returned by AddAutoShape() has X, Y, Width, and Height properties that you can set:
shape.X = 100;
shape.Y = 200;
shape.Width = 400;
shape.Height = 80;Can I set the shape outline (border) color?
Yes, via shape.LineFormat:
using Aspose.Slides.Foss.Drawing;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);Are charts supported?
No. Charts, SmartArt, and OLE objects are not implemented in this edition.