SmartArt in presentations is used to provide the information in visual form. Sometimes, it is opted to make the simple text more appealing. Whereas, in other cases, it is used to demonstrate flow diagrams, processes, relationships between different entities, etc. In this article, you will learn how to create SmartArt in PowerPoint presentations programmatically in C#.

C# .NET API to Create SmartArt in PowerPoint PPT

To work with SmartArt in PowerPoint presentations, we will use Aspose.Slides for .NET. It is a powerful class library to create and manipulate PowerPoint and OpenOffice presentations. You can either install the API via NuGet or download its DLL.

PM> Install-Package Aspose.Slides.NET

Create a SmartArt Shape in PowerPoint PPT in C#

Aspose.Slides for .NET provides the easiest way to create the SmartArt shapes in the presentations. For demonstration, let’s create a SmartArt shape from scratch in a PowerPoint presentation using C#.

The following code sample shows how to create a SmartArt shape in a PowerPoint PPT in C#.

// Create a presentation or load existing one
using (Presentation pres = new Presentation())
{
// Access the presentation slide
ISlide slide = pres.Slides[0];
// Add SmartArt Shape
ISmartArt smart = slide.Shapes.AddSmartArt(0, 0, 400, 400, SmartArtLayoutType.BasicBlockList);
smart.AllNodes[0].TextFrame.Text = "First Block";
smart.AllNodes[1].TextFrame.Text = "Second Block";
// Save presentation
pres.Save("SimpleSmartArt_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

The following screenshot shows the output of the above code sample.

Create SmartArt in PowerPoint C#

Access a SmartArt Shape in PowerPoint using C#

You can also access the SmartArt shapes in the existing PowerPoint presentations. Once accessed, you can modify them as required. The following are the steps to access the SmartArt shapes in PowerPoint presentations using C#.

  • Create a new presentation or load an existing one using Presentation class.
  • Get reference of the desired slide into an ISlide object.
  • Loop through the shapes in the slide using ISlide.Shapes collection.
  • If the shape is of type ISmartArt, then get its reference into an ISmartArt object.
  • If required, filter the SmartArt shapes of a specific layout using ISmartArt.Layout property.

The following code sample shows how to access SmartArt shapes in PPT in C#.

// Load the presentation
using (Presentation pres = new Presentation("AccessSmartArtShape.pptx"))
{
// Iterate through every shape inside desired slide
foreach (IShape shape in pres.Slides[0].Shapes)
{
// Check if shape is of SmartArt type
if (shape is ISmartArt)
{
// Typecast shape to SmartArt
ISmartArt smart = (ISmartArt)shape;
System.Console.WriteLine("Shape Name:" + smart.Name);
// Checking SmartArt Layout
//if (smart.Layout == SmartArtLayoutType.BasicBlockList)
//{
// Console.WriteLine("Do some thing here....");
//}
}
}
}

Change SmartArt Shape’s Style in PowerPoint

Once you have accessed a SmartArt shape, you can change its style as well. The following steps demonstrate how to change the style of the SmartArt shapes in a PowerPoint PPT using C#.

The following code sample shows how to change the style of the SmartArt shapes in PowerPoint presentations.

// Load presentation
using (Presentation presentation = new Presentation("AccessSmartArtShape.pptx"))
{
// Traverse through every shape inside first slide
foreach (IShape shape in presentation.Slides[0].Shapes)
{
// Check if shape is of SmartArt type
if (shape is ISmartArt)
{
// Typecast shape to SmartArt
ISmartArt smart = (ISmartArt)shape;
// Check SmartArt style
if (smart.QuickStyle == SmartArtQuickStyleType.SimpleFill)
{
// Change SmartArt Style
smart.QuickStyle = SmartArtQuickStyleType.Cartoon;
}
// Check SmartArt color type
if (smart.ColorStyle == SmartArtColorType.ColoredFillAccent1)
{
// Change SmartArt color type
smart.ColorStyle = SmartArtColorType.ColorfulAccentColors;
}
}
}
// Save Presentation
presentation.Save("ChangeSmartArtStyle_out.pptx", SaveFormat.Pptx);
}

C# PowerPoint API - Get a Free License

Get a free temporary license and work with PowerPoint SmartArt without evaluation limitations.

Conclusion

In this article, you have learned how to create SmartArt in PowerPoint presentations using C#. Moreover, you have seen how to access the SmartArt shapes and change their styles programmatically. You can explore the documentation to learn more about Aspose.Slides for .NET. In addition, you can ask your questions via our forum.

See Also