How to Create and Format SmartArt in PowerPoint in C#, VB.NET

Although the default layout of an inserted SmartArt graphic may looks perfect, you may also want to make changes to your graphic, such as adding more shapes, changing the text and colors. This article presents how to add a SmartArt graphic to slide and how to modify its elements programmatically using Spire.Presentation in C# and VB.NET.

Follow the steps below to create and format SmartArt:

Step 1: Create a new PowerPoint document.

Presentation pres = new Presentation();

Step 2: Add a SmartArt to the first slide by calling the method Shapes.AppedSmartArt(float x, float y, float width, float height, Spire.Presentation.Diagrams.SmartArtLayoutType layoutType), here we set SmartArtLayoutTpye as Gear.

Spire.Presentation.Diagrams.ISmartArt sa = pres.Slides[0].Shapes.AppendSmartArt(20, 40, 300, 300, Spire.Presentation.Diagrams.SmartArtLayoutType.Gear);

Now, if we save the file, then we'll get the SmartArt as below. This is what the SmartArt looks like by default.

How to Create and Format SmartArt in PowerPoint in C#, VB.NET

Step 3: Design the SmartArt graphic by setting SmartArtStyleType as SubtleEffect, changing the SmartArtColorType to GradientLoopAccent3. SmartArtStyleType enum and SmartArtColorType enum contain almost all style types and color types that are enabled by MS PowerPoint.

sa.Style = Spire.Presentation.Diagrams.SmartArtStyleType.SubtleEffect;
sa.ColorStyle = Spire.Presentation.Diagrams.SmartArtColorType.GradientLoopAccent3;

Step 4: Remove all shapes (nodes) in the SmartArt. In Spire.Presentation, each node represents a shape.

foreach (object a in sa.Nodes)
    sa.Nodes.RemoveNode(a);

Step 5: Add custom shape to SmartArt by AddNode(), the index [0] behind Nodes indicates the shape will be added in the first place, other shapes without index will be added in turn. Here we add two shapes to SmartArt with formatted text.

Spire.Presentation.Diagrams.ISmartArtNode node = sa.Nodes.AddNode();
sa.Nodes[0].TextFrame.Text = "aa";
node = sa.Nodes.AddNode();
node.TextFrame.Text = "bb";
node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.Black;

Step 6: Save and launch the file.

pres.SaveToFile("SmartArtTest1.pptx", FileFormat.Pptx2007);
System.Diagnostics.Process.Start("SmartArtTest1.pptx");

Output:

Here is what a customized SmartArt looks like. It is obvious that we have removed a shape, changed the shape color and changed the text and text color.

How to Create and Format SmartArt in PowerPoint in C#, VB.NET

Full Code:

[C#]
//add a smartart
Presentation pres = new Presentation();
Spire.Presentation.Diagrams.ISmartArt sa = pres.Slides[0].Shapes.AppendSmartArt(20, 40, 300, 300, Spire.Presentation.Diagrams.SmartArtLayoutType.Gear);

//set type and color of smartart
sa.Style = Spire.Presentation.Diagrams.SmartArtStyleType.SubtleEffect;
sa.ColorStyle = Spire.Presentation.Diagrams.SmartArtColorType.GradientLoopAccent3;

//remove all shapes
foreach (object a in sa.Nodes)
    sa.Nodes.RemoveNode(a);

//add two custom shapes with text
Spire.Presentation.Diagrams.ISmartArtNode node = sa.Nodes.AddNode();
sa.Nodes[0].TextFrame.Text = "aa";
node = sa.Nodes.AddNode();
node.TextFrame.Text = "bb";
node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.Black;

//save and launch the file
pres.SaveToFile("SmartArtTest1.pptx", FileFormat.Pptx2007);
System.Diagnostics.Process.Start("SmartArtTest1.pptx");
[VB.NET]
'add a smartart
Dim pres As New Presentation()
Dim sa As Spire.Presentation.Diagrams.ISmartArt = pres.Slides(0).Shapes.AppendSmartArt(20, 40, 300, 300, Spire.Presentation.Diagrams.SmartArtLayoutType.Gear)

'set type and color of smartart
sa.Style = Spire.Presentation.Diagrams.SmartArtStyleType.SubtleEffect
sa.ColorStyle = Spire.Presentation.Diagrams.SmartArtColorType.GradientLoopAccent3

'remove all shapes
For Each a As Object In sa.Nodes
	sa.Nodes.RemoveNode(0)
Next

'add two custom shapes with text
Dim node As Spire.Presentation.Diagrams.ISmartArtNode = sa.Nodes.AddNode()
sa.Nodes(0).TextFrame.Text = "aa"
node = sa.Nodes.AddNode()
node.TextFrame.Text = "bb"
node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.Black

'save and launch the file
pres.SaveToFile("SmartArtTest1.pptx", FileFormat.Pptx2007)
System.Diagnostics.Process.Start("SmartArtTest1.pptx")