Manage SmartArt Shape Node

Add SmartArt Node

Aspose.Slides for C++ has provided the simplest API to manage the SmartArt shapes in an easiest way. The following sample code will help to add node and child node inside SmartArt shape.

  • Create an instance of Presentation class and load the presentation with SmartArt Shape.
  • Obtain the reference of first slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArt if it is SmartArt.
  • Add a new Node in SmartArt shape NodeCollection and set the text in TextFrame.
  • Now, Add a Child Node in newly added SmartArt Node and set the text in TextFrame.
  • Save the Presentation.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/AddNodes_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Add SmartArt BasicProcess
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArt> smart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddSmartArt(10, 10, 400, 300, SmartArtLayoutType::StackedList);
if (smart->get_AllNodes()->get_Count() > 0)
{
// Accessing SmartArt node at index 0
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArtNode> node = smart->get_AllNodes()->AddNode();
// Add Text
node->get_TextFrame()->set_Text(u"Test");
// SharedPtr<ISmartArtNodeCollection> nodeCollection = System::DynamicCast_noexcept<ISmartArtNodeCollection>(node->get_ChildNodes()); ;
auto nodeCollection = node->get_ChildNodes() ;
// Adding new child node at end of parent node
SharedPtr<ISmartArtNode> chNode = nodeCollection->AddNode();
// Add Text
chNode->get_TextFrame()->set_Text(u"Sample Text Added");
}
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Add SmartArt Node at Specific Position

In the following sample code we have explained how to add the child nodes belonging to respective nodes of SmartArt shape at particular position.

  • Create an instance of Presentation class.
  • Obtain the reference of first slide by using its Index.
  • Add a StackedList type SmartArt shape in accessed slide.
  • Access the first node in added SmartArt shape.
  • Now, add the Child Node for selected Node at position 2 and set its text.
  • Save the Presentation.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/AddNodesSpecificPosition_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Add SmartArt BasicProcess
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArt> smart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddSmartArt(10, 10, 400, 300, SmartArtLayoutType::StackedList);
if (smart->get_AllNodes()->get_Count() > 0)
{
// Accessing SmartArt node at index 0
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node0 = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(smart->get_AllNodes()->idx_get(0));
// SharedPtr<ISmartArtNodeCollection> node0Collection = System::DynamicCast_noexcept<ISmartArtNodeCollection>(node0->get_ChildNodes()); ;
SharedPtr<ISmartArtNodeCollection> node0Collection = node0->get_ChildNodes() ;
// Adding new child node at position 2 in parent node
SharedPtr<ISmartArtNode> chNode = node0Collection->AddNodeByPosition(2);
// Add Text
chNode->get_TextFrame()->set_Text(u"Sample Text Added");
}
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Access SmartArt Node

The following sample code will help to access nodes inside SmartArt shape. Please note that you cannot change the LayoutType of the SmartArt as it is read only and is set only when the SmartArt shape is added.

  • Create an instance of Presentation class and load the presentation with SmartArt Shape.
  • Obtain the reference of first slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArt if it is SmartArt.
  • Traverse through all Nodes inside SmartArt Shape.
  • Access and display information like SmartArt Node position, level and Text.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String templatePath = u"../templates/SmartArt.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Traverse through every shape inside first slide
//foreach(IShape shape in pres.Slides[0].Shapes)
for (int x = 0; x<pres->get_Slides()->idx_get(0)->get_Shapes()->get_Count(); x++)
{
SharedPtr<IShape> shape = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(x);
if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
{
System::SharedPtr<Aspose::Slides::SmartArt::SmartArt> smart = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArt>(shape);
// Traverse through all nodes inside SmartArt
for (int i = 0; i < smart->get_AllNodes()->get_Count(); i++)
{
// Accessing SmartArt node at index i
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(smart->get_AllNodes()->idx_get(i));
// Printing the SmartArt node parameters
System::Console::WriteLine(u"j = " + node->get_TextFrame()->get_Text() + u", Text = " + node->get_Level() + u", Position = " + node->get_Position());
}
}
}

Access SmartArt Child Node

The following sample code will help to access the child nodes belonging to respective nodes of SmartArt shape.

  • Create an instance of PresentationEx class and load the presentation with SmartArt Shape.
  • Obtain the reference of first slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArtEx if it is SmartArt.
  • Traverse through all Nodes inside SmartArt Shape.
  • For every selected SmartArt shape Node, traverse through all Child Nodes inside particular node.
  • Access and display information like Child Node position, level and Text.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String templatePath = u"../templates/SmartArt.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Traverse through every shape inside first slide
for(int x=0;x<pres->get_Slides()->idx_get(0)->get_Shapes()->get_Count();x++)
{
SharedPtr<IShape> shape = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(x);
if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
{
System::SharedPtr<Aspose::Slides::SmartArt::SmartArt> smart = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArt>(shape);
// Traverse through all nodes inside SmartArt
for (int i = 0; i < smart->get_AllNodes()->get_Count(); i++)
{
// Accessing SmartArt node at index i
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node0 = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(smart->get_AllNodes()->idx_get(i));
// Traversing through the child nodes in SmartArt node at index i
for (int j = 0; j < node0->get_ChildNodes()->get_Count(); j++)
{
// Accessing the child node in SmartArt node
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(node0->get_ChildNodes()->idx_get(j));
// Printing the SmartArt child node parameters
System::Console::WriteLine(u"j = " + node->get_TextFrame()->get_Text()+u", Text = "+ node->get_Level()+u", Position = "+ node->get_Position());
}
}
}
}

Access SmartArt Child Node at Specific Position

In this example, we will learn to access the child nodes at some particular position belonging to respective nodes of SmartArt shape.

  • Create an instance of Presentation class.
  • Obtain the reference of first slide by using its Index.
  • Add a StackedList type SmartArt shape.
  • Access the added SmartArt shape.
  • Access the node at index 0 for accessed SmartArt shape.
  • Now, access the Child Node at position 1 for accessed SmartArt node using GetNodeByPosition() method.
  • Access and display information like Child Node position, level and Text.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/AccessChildNodeSpecificPosition_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Add SmartArt BasicProcess
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArt> smart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddSmartArt(10, 10, 400, 300, SmartArtLayoutType::StackedList);
if (smart->get_AllNodes()->get_Count() > 0)
{
// Accessing SmartArt node at index 0
SharedPtr<Aspose::Slides::SmartArt::ISmartArtNode> node0 = smart->get_AllNodes()->idx_get(0);
//Accessing child node collection
auto nodeCollection =node0->get_ChildNodes();
SharedPtr<SmartArtNode> foundChild;
int position = 1;
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArtNode> node = node0->get_ChildNodes()->idx_get(position);
// Printing the SmartArt child node parameters
System::Console::WriteLine(u"j = " + node->get_TextFrame()->get_Text() + u", Text = " + node->get_Level() + u", Position = " + node->get_Position());
}

Remove SmartArt Node

In this example, we will learn to remove the nodes inside SmartArt shape.

  • Create an instance of Presentation class and load the presentation with SmartArt Shape.
  • Obtain the reference of first slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArt if it is SmartArt.
  • Check if the SmartArt has more than 0 nodes.
  • Select the SmartArt node to be deleted.
  • Now, remove the selected node using RemoveNode() method* Save the Presentation.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String templatePath = u"../templates/SmartArt.pptx";
const String outPath = u"../out/RemoveNode_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Traverse through every shape inside first slide
for (int x = 0; x < pres->get_Slides()->idx_get(0)->get_Shapes()->get_Count(); x++)
{
SharedPtr<IShape> shape = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(x);
if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
{
System::SharedPtr<Aspose::Slides::SmartArt::SmartArt> smart = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArt>(shape);
if (smart->get_AllNodes()->get_Count() > 0)
{
// Accessing SmartArt node at index 0
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node0 = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(smart->get_AllNodes()->idx_get(0));
// Removing the selected node
smart->get_AllNodes()->RemoveNode(node0);
}
}
}
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Remove SmartArt Node at Specific Position

In this example, we will learn to remove the nodes inside SmartArt shape at particular position.

  • Create an instance of Presentation class and load the presentation with SmartArt Shape.
  • Obtain the reference of first slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArt if it is SmartArt.
  • Select the SmartArt shape node at index 0.
  • Now, check if the selected SmartArt node has more than 2 child nodes.
  • Now, remove the node at Position 1 using RemoveNodeByPosition() method.
  • Save the Presentation.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String templatePath = u"../templates/SmartArt.pptx";
const String outPath = u"../out/RemoveSmartArtNodeByPosition_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Traverse through every shape inside first slide
for (int x = 0; x < pres->get_Slides()->idx_get(0)->get_Shapes()->get_Count(); x++)
{
SharedPtr<IShape> shape = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(x);
if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
{
System::SharedPtr<Aspose::Slides::SmartArt::SmartArt> smart = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArt>(shape);
if (smart->get_AllNodes()->get_Count() > 0)
{
// Accessing SmartArt node at index 0
System::SharedPtr<Aspose::Slides::SmartArt::SmartArtNode> node0 = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArtNode>(smart->get_AllNodes()->idx_get(0));
if (node0->get_ChildNodes()->get_Count() >= 2)
{
// Removing the child node at position 1
node0->get_ChildNodes()->RemoveNode(1);
}
}
}
}
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Set Custom Position for SmartArt Child Node

Now Aspose.Slides for .NET support for setting SmartArtShape X and Y properties. The code snippet below shows how to set custom SmartArtShape position, size and rotation also please note that adding new nodes causes a recalculation of the positions and sizes of all nodes.

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
System::SharedPtr<ISmartArt> smart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddSmartArt(20, 20, 600, 500, Aspose::Slides::SmartArt::SmartArtLayoutType::OrganizationChart);
// Move SmartArt shape to new position
System::SharedPtr<ISmartArtNode> node = smart->get_AllNodes()->idx_get(1);
System::SharedPtr<ISmartArtShape> shape = node->get_Shapes()->idx_get(1);
shape->set_X((float)(shape->get_X() + (shape->get_Width() * 2)));
shape->set_Y((float)(shape->get_Y() - (shape->get_Height() / 2)));
// Change SmartArt shape's widths
node = smart->get_AllNodes()->idx_get(2);
shape = node->get_Shapes()->idx_get(1);
shape->set_Width(shape->get_Width() + (shape->get_Width() / 2));
// Change SmartArt shape's height
node = smart->get_AllNodes()->idx_get(3);
shape = node->get_Shapes()->idx_get(1);
shape->set_Height(shape->get_Height() + (shape->get_Height() / 2));
// Change SmartArt shape's rotation
node = smart->get_AllNodes()->idx_get(4);
shape = node->get_Shapes()->idx_get(1);
shape->set_Rotation(90);
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Check Assistant Node

In the following sample code we will investigate how to identify Assistant Nodes in the SmartArt nodes collection and changing them.

  • Create an instance of PresentationEx class and load the presentation with SmartArt Shape.
  • Obtain the reference of second slide by using its Index.
  • Traverse through every shape inside first slide.
  • Check if shape is of SmartArt type and Typecast selected shape to SmartArtEx if it is SmartArt.
  • Traverse through all nodes inside SmartArt shape and check if they are Assistant Nodes.
  • Change the status of Assistant Node to normal node.
  • Save the Presentation.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String templatePath = u"../templates/AssistantNode.pptx";
const String outPath = u"../out/ChangeAssitantNode_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath);
// Traverse through every shape inside first slide
for (int x = 0; x < pres->get_Slides()->idx_get(0)->get_Shapes()->get_Count(); x++)
{
SharedPtr<IShape> shape = pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(x);
if (System::ObjectExt::Is<Aspose::Slides::SmartArt::SmartArt>(shape))
{
System::SharedPtr<Aspose::Slides::SmartArt::SmartArt> smart = System::DynamicCast_noexcept<Aspose::Slides::SmartArt::SmartArt>(shape);
for (int i = 0; i < smart->get_AllNodes()->get_Count(); i++)
{
// Accessing SmartArt node at index i
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArtNode> node = smart->get_AllNodes()->idx_get(i);
if (node->get_IsAssistant())
{
// Setting Assitant node to false and making it normal node
node->set_IsAssistant(false);
}
}
}
}
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Set Node’s Fill Format

Aspose.Slides for C++ makes it possible to add custom SmartArt shapes and set their fill formats. This article explains how to create and access SmartArt shapes and set their fill format using Aspose.Slides for C++.

Please follow the steps below:

  • Create an instance of the Presentation class.
  • Obtain the reference of a slide using its index.
  • Add a SmartArt shape by setting its LayoutType.
  • Set the FillFormat for the SmartArt shape nodes.
  • Write the modified presentation as a PPTX file.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/FillFormat_SmartArt_ShapeNode_out.pptx";
// Load the desired the presentation
SharedPtr<Presentation> pres = MakeObject<Presentation>();
// Add SmartArt BasicProcess
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArt> smart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddSmartArt(10, 10, 400, 300, SmartArtLayoutType::ClosedChevronProcess);
// Adding SmartArt node
System::SharedPtr<Aspose::Slides::SmartArt::ISmartArtNode> NewNode = smart->get_AllNodes()->AddNode();
//Adding text to added node
NewNode->get_TextFrame()->set_Text( u"Some text");
// Save Presentation
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Generate Thumbnail of SmartArt Child Node

Developers can generate a thumbnail of Child node of a SmartArt by following the steps below:

  1. Instantiate Presentation class that represents the PPTX file.
  2. Add SmartArt.
  3. Obtain the reference of a node by using its Index
  4. Get the thumbnail image.
  5. Save the thumbnail image in any desired image format.

The example below generating a thumbnail of SmartArt child node

auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto smartArt = slide->get_Shapes()->AddSmartArt(10, 10, 400, 300, SmartArtLayoutType::BasicCycle);
auto node = smartArt->get_Node(1);

auto image = node->get_Shape(0)->GetImage();
image->Save(u"SmartArt_ChildNote_Thumbnail_out.jpeg", ImageFormat::Png);
image->Dispose();

presentation->Dispose();