Manage SmartArt Shape
Create SmartArt Shape
Aspose.Slides for PHP via Java has provided an API to create SmartArt shapes. To create a SmartArt shape in a slide, please follow the steps below:
- Create an instance of Presentation class.
- Obtain the reference of a slide by using its Index.
- Add a SmartArt shape by setting it LayoutType.
- Save the modified presentation as a PPTX file.
# Instantiate Presentation Class
$pres = new Presentation();
try {
# Get first slide
$slide = $pres->getSlides()->get_Item(0);
# Add Smart Art Shape
$smart = $slide->getShapes()->addSmartArt(0, 0, 400, 400, SmartArtLayoutType::BasicBlockList);
# Saving presentation
$pres->save("SimpleSmartArt.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Figure: SmartArt shape added to the slide |
Access SmartArt Shape in Slide
The following code will be used to access the SmartArt shapes added in presentation slide. In sample code we will traverse through every shape inside the slide and check if it is a SmartArt shape. If shape is of SmartArt type then we will typecast that to SmartArt instance.
# Load the desired the presentation
$pres = new Presentation("AccessSmartArtShape.pptx");
try {
# Traverse through every shape inside first slide
foreach($pres->getSlides()->get_Item(0)->getShapes() as $shape) {
# Check if shape is of SmartArt type
if (java_instanceof($shape, new JavaClass("com.aspose.slides.SmartArt"))) {
# Typecast shape to SmartArtEx
$smart = $shape;
echo("Shape Name:" . $smart->getName());
}
}
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Access SmartArt Shape with Particular Layout Type
The following sample code will help to access the SmartArt shape with particular LayoutType:: 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.
- Check the SmartArt shape with particular LayoutType and perform what is required to be done afterwards.
$pres = new Presentation("AccessSmartArtShape.pptx");
try {
# Traverse through every shape inside first slide
foreach($pres->getSlides()->get_Item(0)->getShapes() as $shape) {
# Check if shape is of SmartArt type
if (java_instanceof($shape, new JavaClass("com.aspose.slides.SmartArt"))) {
# Typecast shape to SmartArtEx
$smart = $shape;
# Checking SmartArt Layout
if ($smart->getLayout() == SmartArtLayoutType::BasicBlockList) {
echo("Do some thing here....");
}
}
}
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Change SmartArt Shape Style
In this example, we will learn to change the quick style for any 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.
- Find the SmartArt shape with particular Style.
- Set the new Style for the SmartArt shape.
- Save the Presentation.
# Instantiate Presentation Class
$pres = new Presentation("SimpleSmartArt.pptx");
try {
# Get first slide
$slide = $pres->getSlides()->get_Item(0);
# Traverse through every shape inside first slide
foreach($slide->getShapes() as $shape) {
# Check if shape is of SmartArt type
if (java_instanceof($shape, new JavaClass("com.aspose.slides.SmartArt"))) {
# Typecast shape to SmartArtEx
$smart = $shape;
# Checking SmartArt style
if ($smart->getQuickStyle() == SmartArtQuickStyleType::SimpleFill) {
# Changing SmartArt Style
$smart->setQuickStyle(SmartArtQuickStyleType::Cartoon);
}
}
}
# Saving presentation
$pres->save("ChangeSmartArtStyle.pptx", SaveFormat::Pptx);
} finally {
$pres->dispose();
}
Figure: SmartArt shape with changed Style |
Change SmartArt Shape Color Style
In this example, we will learn to change the color style for any SmartArt shape. In the following sample code will access the SmartArt shape with particular color style and will change its style.
- 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.
- Find the SmartArt shape with particular Color Style.
- Set the new Color Style for the SmartArt shape.
- Save the Presentation.
# Instantiate Presentation Class
$pres = new Presentation("SimpleSmartArt.pptx");
try {
# Get first slide
$slide = $pres->getSlides()->get_Item(0);
# Traverse through every shape inside first slide
foreach($slide->getShapes() as $shape) {
# Check if shape is of SmartArt type
if (java_instanceof($shape, new JavaClass("com.aspose.slides.SmartArt"))) {
# Typecast shape to SmartArtEx
$smart = $shape;
# Checking SmartArt color type
if ($smart->getColorStyle() == SmartArtColorType::ColoredFillAccent1) {
# Changing SmartArt color type
$smart->setColorStyle(SmartArtColorType::ColorfulAccentColors);
}
}
}
# Saving presentation
$pres->save("ChangeSmartArtColorStyle.pptx", SaveFormat::Pptx);
} finally {
$pres->dispose();
}
Figure: SmartArt shape with changed Color Style |