Manage SmartArt Shape
Create SmartArt Shape
Aspose.Slides for 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
Presentation pres = new Presentation();
try {
// Get first slide
ISlide slide = pres.getSlides().get_Item(0);
// Add Smart Art Shape
ISmartArt smart = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.BasicBlockList);
// Saving presentation
pres.save("SimpleSmartArt.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) 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
Presentation pres = new Presentation("AccessSmartArtShape.pptx");
try {
// Traverse through every shape inside first slide
for (IShape shape : pres.getSlides().get_Item(0).getShapes())
{
// Check if shape is of SmartArt type
if (shape instanceof ISmartArt)
{
// Typecast shape to SmartArtEx
ISmartArt smart = (ISmartArt)shape;
System.out.println("Shape Name:" + smart.getName());
}
}
} finally {
if (pres != null) 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.
Presentation pres = new Presentation("AccessSmartArtShape.pptx");
try {
// Traverse through every shape inside first slide
for (IShape shape : pres.getSlides().get_Item(0).getShapes())
{
// Check if shape is of SmartArt type
if (shape instanceof ISmartArt)
{
// Typecast shape to SmartArtEx
ISmartArt smart = (ISmartArt) shape;
// Checking SmartArt Layout
if (smart.getLayout() == SmartArtLayoutType.BasicBlockList)
{
System.out.println("Do some thing here....");
}
}
}
} finally {
if (pres != null) 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
Presentation pres = new Presentation("SimpleSmartArt.pptx");
try {
// Get first slide
ISlide slide = pres.getSlides().get_Item(0);
// Traverse through every shape inside first slide
for (IShape shape : slide.getShapes())
{
// Check if shape is of SmartArt type
if (shape instanceof ISmartArt)
{
// Typecast shape to SmartArtEx
ISmartArt smart = (ISmartArt) 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
Presentation pres = new Presentation("SimpleSmartArt.pptx");
try {
// Get first slide
ISlide slide = pres.getSlides().get_Item(0);
// Traverse through every shape inside first slide
for (IShape shape : slide.getShapes())
{
// Check if shape is of SmartArt type
if (shape instanceof ISmartArt)
{
// Typecast shape to SmartArtEx
ISmartArt smart = (ISmartArt) 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 |