Home > Articles > .Net Framework Articles > How to add and format SmartArt shape on PowerPoint document

How to add and format SmartArt shape on PowerPoint document

by Jane Smith   on Jul 25, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 681    |  Points: 100   
Like this article? Bookmark and Share:
Add custom SmartArt shapes to the slides from scratch. Set the style type, color style for the SmartArt shape and extract text from SmartArt in PowerPoint in C#.

Introduction


We always use SmartArt shape on the PowerPoint document file to make our presentation more vivid and beautiful. Spire.Presentation for .NET now enables developers to add custom SmartArt shapes in their slides from scratch. With the help of Spire.Presentation for .NET, we can set the style type, color style for the SmartArt shape and we can also extract text from SmartArt in PowerPoint in C#. In this article we are going to see how to add and format SmartArt shape on PowerPoint document files in C#.

What is Spire.Presentation?

Spire.Presentation is a .NET PowerPoint library which enables developers to operate PowerPoint documents from C# and VB.NET. It doesn't require Microsoft PowerPoint to be installed on the system. You can download to get the Spire.Presentation.dll from here: http://www.nuget.org/packages/Spire.Presentation/

Tools we need:

- Spire.Presentation.dll
- Visual Studio

Namespaces to be used

using Spire.Presentation;

using Spire.Presentation.Diagrams;

using System.IO;

Step 1: Create a new instance of presentation document.

Presentation ppt = new Presentation();

Step 2: Add a SmartArt shape to the first slide by calling the method Shapes.AppedSmartArt(); here we set SmartArtLayoutTpye as Gear.

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

Step 3: Design the SmartArt graphic by setting the SmartArt style type and color type.

sa.Style = Spire.Presentation.Diagrams.SmartArtStyleType.SubtleEffect;

sa.ColorStyle = Spire.Presentation.Diagrams.SmartArtColorType.GradientLoopAccent3;

Step 4: Remove all default shapes and add two custom shapes with text.

foreach (object a in sa.Nodes)

    sa.Nodes.RemoveNode(0);

 

Spire.Presentation.Diagrams.ISmartArtNode node = sa.Nodes.AddNode();

sa.Nodes[0].TextFrame.Text = "SmartArt1";

node = sa.Nodes.AddNode();

node.TextFrame.Text = "SmartArt2";

node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;

node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.Black;

Step 5: Save the document to file.

string output = "result.pptx";

ppt.SaveToFile(output, FileFormat.Pptx2010);

Here we will find that we have added and formatted the SmartArt shape to the PowerPoint document files. Please check the effective screenshot:


When we work with SmartArt shapes, we may need to extract the texts from the SmartArt shape. Now I will further introduce how to extract text from SmartArt from the PowerPoint document file from code.

Step 1: Load the resulted PowerPoint document with SmartArt shape.

ppt.LoadFromFile("result.pptx");

Step 2: Create a new instance of StringBulider class, traverse through all the slides of the .pptx file, find the SmartArt shapes, and then extract text from SmartArt shape Nodes and append to the StringBuilder object.

StringBuilder st = new StringBuilder();

for (int i = 0; i < ppt.Slides.Count; i++)

{

    for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)

    {

        if (ppt.Slides[i].Shapes[j] is ISmartArt)

        {

            ISmartArt smartArt = ppt.Slides[i].Shapes[j] as ISmartArt;

            for (int k = 0; k < smartArt.Nodes.Count; k++)

            {

                st.Append(smartArt.Nodes[k].TextFrame.Text);

            }

        }

    }

}

Step 3: Create a new text file and write the extracted text to the text file.

File.WriteAllText("Result.txt", st.ToString());

Check the resulted text file in .txt format:


Conclusion:

Generally speaking, Spire.Presentation supports work well with the SmartArt and it also supports to save the presentation slides with SmartArt into the other widely used file formats, such as Images and PDF. It worthy your try on it.

 



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts