SmartArt is a built-in feature in PowerPoint that can graphically represent concepts such as hierarchies, processes, relationships and matrices. With SmartArt, you can explain the complex information in an easily understandable manner. In this article, you will learn how to programmatically add a SmartArt graphic to a PowerPoint slide and custom its layout using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Create a SmartArt in PowerPoint
The detailed steps are as follows:
- Create a Presentation instance.
- Get a specified slide using Presentation.Slides[] property.
- Insert a SmartArt graphic into the specified slide using ISlide.Shapes.AppendSmartArt(float x, float y, float width, float height, SmartArtLayoutType layoutType) method.
- Set the style and color of the SmartArt using ISmartArt.Style and ISmartArt.ColorStyle properties.
- Loop through the nodes in SmartArt and remove all default nodes using ISmartArt.Nodes.RemoveNode() method.
- Add two nodes to the SmartArt using ISmartArt.Nodes.AddNode() method, and then add text to each node using ISmartArtNode.TextFrame.Text property.
- Get the text range in a specified node using ISmartArtNode.TextFrame.TextRange property, then set the text fill type and color using TextRange.Fill.FillType and TextRange.Fill.SolidColor.KnownColor properties.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Diagrams; using Spire.Presentation.Drawing; namespace AddSmartArt { class Program { static void Main(string[] args) { //Create a Presentation instance Presentation presentation = new Presentation(); //Get the first slide ISlide slide = presentation.Slides[0]; //Add a SmartArt graphic to the slide ISmartArt smartArt = slide.Shapes.AppendSmartArt(20, 40, 300, 300, SmartArtLayoutType.Gear); //Set the style and color of SmartArt smartArt.Style = SmartArtStyleType.SubtleEffect; smartArt.ColorStyle = SmartArtColorType.GradientLoopAccent3; //Remove all default nodes foreach (object a in smartArt.Nodes) smartArt.Nodes.RemoveNode(0); //Add two nodes with text ISmartArtNode node1 = smartArt.Nodes.AddNode(); node1.TextFrame.Text = "Optimization"; ISmartArtNode node2 = smartArt.Nodes.AddNode(); node2.TextFrame.Text = "Design"; // Set the fill type and color of the text in first node node1.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid; node1.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.DarkRed; //Save the result file presentation.SaveToFile("AddSmartArt.pptx", FileFormat.Pptx2007); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.