管理智能艺术形状节点

添加智能艺术节点

Aspose.Slides for C++ 提供了最简单的API,用于以最简单的方式管理智能艺术形状。以下示例代码将帮助您在智能艺术形状中添加节点和子节点。

  • 创建一个 Presentation 类的实例,并加载包含智能艺术形状的演示文稿。
  • 通过使用索引获取第一张幻灯片的引用。
  • 遍历第一张幻灯片中的每个形状。
  • 检查形状是否为智能艺术类型,如果是智能艺术,则将所选形状强制转换为智能艺术。
  • 在智能艺术形状的 NodeCollection 中添加一个新节点,并在 TextFrame 中设置文本。
  • 现在,在新添加的智能艺术节点中添加一个子节点,并在 TextFrame 中设置文本。
  • 保存演示文稿。
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);

在特定位置添加智能艺术节点

在以下示例代码中,我们解释了如何在特定位置添加与智能艺术形状相应节点的子节点。

  • 创建一个 Presentation 类的实例。
  • 通过使用索引获取第一张幻灯片的引用。
  • 在访问的幻灯片中添加一个 StackedList 类型的智能艺术形状。
  • 访问添加的智能艺术形状中的第一个节点。
  • 现在,在所选节点的第2个位置添加子节点并设置其文本。
  • 保存演示文稿。
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);

访问智能艺术节点

以下示例代码将帮助您访问智能艺术形状中的节点。请注意,您无法更改智能艺术的 LayoutType,因为它是只读的,并且仅在添加智能艺术形状时设置。

  • 创建一个 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";
// 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());
}
}
}

访问智能艺术子节点

以下示例代码将帮助您访问属于智能艺术形状相应节点的子节点。

  • 创建一个 PresentationEx 类的实例,并加载包含智能艺术形状的演示文稿。
  • 通过使用索引获取第一张幻灯片的引用。
  • 遍历第一张幻灯片中的每个形状。
  • 检查形状是否为智能艺术类型,如果是智能艺术,请将所选形状强制转换为 SmartArtEx。
  • 遍历智能艺术形状中的所有节点。
  • 对于每个选定的智能艺术形状节点,遍历特定节点中的所有子节点。
  • 访问并显示信息,如子节点的位置、级别和文本。
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());
}
}
}
}

在特定位置访问智能艺术子节点

在这个例子中,我们将学习如何在特定位置访问属于智能艺术形状相应节点的子节点。

  • 创建一个 Presentation 类的实例。
  • 通过使用索引获取第一张幻灯片的引用。
  • 添加一个 StackedList 类型的智能艺术形状。
  • 访问添加的智能艺术形状。
  • 访问访问的智能艺术形状的索引0处的节点。
  • 现在,使用 GetNodeByPosition() 方法访问访问的智能艺术节点的第1个位置的子节点。
  • 访问并显示信息,如子节点的位置、级别和文本。
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());
}

删除智能艺术节点

在这个例子中,我们将学习如何删除智能艺术形状中的节点。

  • 创建一个 Presentation 类的实例,并加载包含智能艺术形状的演示文稿。
  • 通过使用索引获取第一张幻灯片的引用。
  • 遍历第一张幻灯片中的每个形状。
  • 检查形状是否为智能艺术类型,如果是智能艺术,则将所选形状强制转换为智能艺术。
  • 检查智能艺术是否有超过0个节点。
  • 选择要删除的智能艺术节点。
  • 现在,使用 RemoveNode() 方法删除所选节点* 保存演示文稿。
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);

在特定位置删除智能艺术节点

在这个例子中,我们将学习如何在特定位置删除智能艺术形状中的节点。

  • 创建一个 Presentation 类的实例,并加载包含智能艺术形状的演示文稿。
  • 通过使用索引获取第一张幻灯片的引用。
  • 遍历第一张幻灯片中的每个形状。
  • 检查形状是否为智能艺术类型,如果是智能艺术,则将所选形状强制转换为智能艺术。
  • 选择索引0处的智能艺术形状节点。
  • 现在,检查所选的智能艺术节点是否有超过2个子节点。
  • 现在,使用 RemoveNodeByPosition() 方法删除位置1处的节点。
  • 保存演示文稿。
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);

为智能艺术子节点设置自定义位置

现在Aspose.Slides for .NET支持设置智能艺术形状的X和Y属性。下面的代码片段展示了如何设置自定义智能艺术形状的位置、大小和旋转,请注意,添加新节点会导致所有节点的位置和大小重新计算。

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);

检查助手节点

在以下示例代码中,我们将研究如何识别智能艺术节点集合中的助手节点并进行更改。

  • 创建一个 PresentationEx 类的实例,并加载包含智能艺术形状的演示文稿。
  • 通过使用索引获取第二张幻灯片的引用。
  • 遍历第一张幻灯片中的每个形状。
  • 检查形状是否为智能艺术类型,如果是智能艺术,则将所选形状强制转换为 SmartArtEx。
  • 遍历智能艺术形状中的所有节点,检查它们是否为助手节点。
  • 将助手节点的状态更改为正常节点。
  • 保存演示文稿。
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);

设置节点的填充格式

Aspose.Slides for C++ 使添加自定义智能艺术形状并设置其填充格式成为可能。本文解释了如何创建和访问智能艺术形状,并使用 Aspose.Slides for C++ 设置其填充格式。

请按照以下步骤操作:

  • 创建一个 Presentation 类的实例。
  • 使用索引获取幻灯片的引用。
  • 通过设置其 LayoutType 添加智能艺术形状。
  • 为智能艺术形状节点设置 FillFormat。
  • 将修改后的演示文稿写入PPTX文件。
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);

生成智能艺术子节点的缩略图

开发人员可以通过以下步骤生成智能艺术子节点的缩略图:

  1. 实例化一个 Presentation 类,该类表示PPTX文件。
  2. 添加智能艺术。
  3. 通过使用索引获取节点的引用。
  4. 获取缩略图图像。
  5. 以任何所需的图像格式保存缩略图图像。

以下示例生成智能艺术子节点的缩略图

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();