News Category

SmartArt

SmartArt (4)

This article demonstrates how to add hyperlinks to SmartArt Nodes in a PowerPoint document in C# and VB.NET using Spire.Presentation for .NET.

C#
using Spire.Presentation;
using Spire.Presentation.Diagrams;

namespace SmartArt
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("SmartArt.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];
           
            //Get the SmartArt
            ISmartArt smartArt = slide.Shapes[0] as ISmartArt;

            //Add hyperlink to the first node of the SmartArt to link to a web page
            smartArt.Nodes[0].Click = new ClickHyperlink("https://www.e-iceblue.com");
            //Add hyperlink to the first node of the SmartArt to link to a specific slide
            smartArt.Nodes[1].Click = new ClickHyperlink(ppt.Slides[1]);

            //Save the result document
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013);
        }
    }
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Diagrams

Namespace SmartArt
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim ppt As Presentation = New Presentation()
            ppt.LoadFromFile("SmartArt.pptx")
            Dim slide As ISlide = ppt.Slides(0)
            Dim smartArt As ISmartArt = TryCast(slide.Shapes(0), ISmartArt)
            smartArt.Nodes(0).Click = New ClickHyperlink("https://www.e-iceblue.com")
            smartArt.Nodes(1).Click = New ClickHyperlink(ppt.Slides(1))
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

Output:

Add Hyperlinks to SmartArt Nodes in PowerPoint in C#, VB.NET

With the help of Spire.Presentation, we can easily add SmartArt shape to the presentation slides. We can add a new node to the existing SmartArt shape to presentation slides and the following code example demonstrates the same.

Step 1: Create a new PowerPoint document and load the sample document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Get the SmartArt from the presentation slide.

ISmartArt sa = presentation.Slides[0].Shapes[1] as ISmartArt;

Step 3: Add a node.

ISmartArtNode node = sa.Nodes.AddNode();

Step 4: Add text and set the text style for the node.

node.TextFrame.Text = "NewStep";
node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.HotPink;

Step 5: Save the document to file.

presentation.SaveToFile("AddNode.pptx", FileFormat.Pptx2010);

Effective screenshot after adding a new node:

Add a Node to SmartArt in PowerPoint in C#

Full codes of adding a node to SmartArt:

using Spire.Presentation;
using Spire.Presentation.Diagrams;
namespace AddNote
{

    class Program
    {

        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("Sample.pptx");

            ISmartArt sa = presentation.Slides[0].Shapes[1] as ISmartArt;

            ISmartArtNode node = sa.Nodes.AddNode();

            node.TextFrame.Text = "NewStep";
            node.TextFrame.TextRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            node.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.HotPink;

            presentation.SaveToFile("AddNode.pptx", FileFormat.Pptx2010);


        }
    }
}

Spire.Presentation for .NET provides two flexible methods: RemoveNode() and RemoveNodeByPosition() for developers to remove nodes from SmartArt. In this article, we will learn how to remove a specific node by position from SmartArt in PowerPoint using the RemoveNodeByPosition() method.

Below is the screenshot of the original SmartArt:

How to remove a specific node by position from SmartArt in PowerPoint

Detail steps:

Step 1: Create a new instance of Presentation class and load the PPT file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("SmartArt.pptx");

Step 2: Get the SmartArt and collect nodes.

ISmartArt smartart = presentation.Slides[0].Shapes[0] as ISmartArt;
ISmartArtNodeCollection nodes = smartart.Nodes;

Step 3: Call nodes.RemoveNodeByPosition(int position) method to remove the specific node by position.

nodes.RemoveNodeByPosition(0);

Step 4: Save the file to disk.

presentation.SaveToFile("RemoveNodes.pptx", FileFormat.Pptx2010);

Running the project, we'll get the following result SmartArt:

How to remove a specific node by position from SmartArt in PowerPoint

Full codes:

[C#]
using Spire.Presentation;
using Spire.Presentation.Diagrams;

namespace Remove_Node_from_SmartArt_in_PowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("SmartArt.pptx");

            ISmartArt smartart = presentation.Slides[0].Shapes[0] as ISmartArt;
            ISmartArtNodeCollection nodes = smartart.Nodes;

            nodes.RemoveNodeByPosition(0);

            presentation.SaveToFile("RemoveNodes.pptx", FileFormat.Pptx2010);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Diagrams

Namespace Remove_Node_from_SmartArt_in_PowerPoint
	Class Program
		Private Shared Sub Main(args As String())
			Dim presentation As New Presentation()
			presentation.LoadFromFile("SmartArt.pptx")

			Dim smartart As ISmartArt = TryCast(presentation.Slides(0).Shapes(0), ISmartArt)
			Dim nodes As ISmartArtNodeCollection = smartart.Nodes

			nodes.RemoveNodeByPosition(0)

			presentation.SaveToFile("RemoveNodes.pptx", FileFormat.Pptx2010)
		End Sub
	End Class
End Namespace

Spire.Presentation as a powerful PowerPoint library, enables developers to create SmartArt, add text to SmartArt and format SmartArt in a flexible way (Reference: How to Create and Format SmartArt in PowerPoint in C#, VB.NET). In this article, we will further introduce how to extract text from SmartArt in PowerPoint in C# and VB.NET by using Spire.Presentation for .NET.

Please look at the appearance of the sample PPT file first:

How to Extract Text from SmartArt in PowerPoint in C#, VB.NET

Detail steps and code snippets are as below:

Step 1: Create a new instance of Presentation class and load the sample PPT file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");

Step 2: Create a new instance of StringBulider class, traverse through all the slides of the PPT 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());

The result text file:

How to Extract Text from SmartArt in PowerPoint in C#, VB.NET

Full codes:

[C#]
using System.IO;
using System.Text;
using Spire.Presentation;
using Spire.Presentation.Diagrams;

namespace Extract_text_from_SmartArt_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Sample.pptx");

            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);
                        }
                    }
                }
            }
            File.WriteAllText("Result.txt", st.ToString());
       }
    }
}
[VB.NET]
Imports System.IO
Imports System.Text
Imports Spire.Presentation
Imports Spire.Presentation.Diagrams

Namespace Extract_text_from_SmartArt_in_PPT
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("Sample.pptx")

			Dim st As New StringBuilder()
			For i As Integer = 0 To ppt.Slides.Count - 1
				For j As Integer = 0 To ppt.Slides(i).Shapes.Count - 1
					If TypeOf ppt.Slides(i).Shapes(j) Is ISmartArt Then
						Dim smartArt As ISmartArt = TryCast(ppt.Slides(i).Shapes(j), ISmartArt)
						For k As Integer = 0 To smartArt.Nodes.Count - 1							st.Append(smartArt.Nodes(k).TextFrame.Text)
						Next
					End If
				Next
			Next
			File.WriteAllText("Result.txt", st.ToString())
		End Sub
	End Class
End Namespace