Shapes in PowerPoint presentations are used to elaborate a particular point or create diagrams such as flowcharts, etc. Moreover, the inclusion of shapes makes the presentation more appealing. Therefore, PowerPoint provides a number of shapes such as line, ellipse, rectangle, etc. In this article, you will learn how to automate the manipulation of shapes in PowerPoint presentations. Particularly, the article will cover how to add, connect, clone, or remove shapes in PowerPoint presentations using Java.
- Java API to Work with PowerPoint Shapes
- Types of PowerPoint Shapes
- Add Shape to PowerPoint Slides
- Add Connector to Shapes in PowerPoint Slides
- Clone a Shape in PowerPoint Slides
- Remove Shapes from PowerPoint Slides
- Get a Free API License
Java API to Work with PowerPoint Shapes
In order to work with PowerPoint shapes, we’ll use Aspose.Slides for Java. It is a powerful Java API that lets you manipulate PowerPoint presentations seamlessly. Particularly, the API allows you to create, modify, read, and convert the presentation documents. You can either download the API or get it installed using the following Maven configuration.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.3</version>
<classifier>jdk16</classifier>
</dependency>
Types of PowerPoint Shapes
PowerPoint supports a variety of shapes that you can add to the presentation slides. Accordingly, Aspose.Slides for Java supports the following shape types:
Add Shape to PowerPoint Slides in Java
Aspose.Slides for Java provides ShapeType class to specify the type of shape you want to add. The following are the steps to add a particular shape in a PowerPoint presentation using Java.
- First, create an instance of the Presentation class to create a new or load an existing PowerPoint presentation.
- Get the reference of the slide into an ISlide object.
- Add shape using ISlide.getShapes().addAutoShape(ShapeType.Ellipse, float, float, float, float) method.
- Finally, save the presentation using Presentation.save(String, SaveFormat.Pptx) method.
The following code sample shows how to add a shape to a PowerPoint presentation using Java.
The following is the output you will get after executing this code.
Add Connector between PowerPoint Shapes using Java
The connector is a line that is used to join two shapes. Moreover, a connector could be a straight or a curved line. The following are the steps to add a connector between two PowerPoint shapes.
- First, create an instance of the Presentation class to load the presentation.
- Get collection of the shapes into an IShapeCollection object using Presentation.getSlides().get_Item(0).getShapes() method.
- Add two shapes using IShapeCollection.addAutoShape(ShapeType.Ellipse, float, float, float, float) method and get their references in IAutoShape objects.
- Create a connector using IShapeCollection.addConnector(ShapeType.BentConnector2, float, float, float, float) method and get its reference into IConnector object.
- Join the shapes using IConnector.setStartShapeConnectedTo(IAutoShape) and IConnector.setEndShapeConnectedTo(IAutoShape) methods.
- Finally, save the presentation using Presentation.save(String, SaveFormat.Pptx) method.
The following code sample shows how to add connector between two shapes in PowerPoint using Java.
The following is the screenshot of the PowerPoint presentation after connecting the shapes.
Clone a PowerPoint Shape using Java
You can also clone a PowerPoint shape to create its copy. The following are the steps to perform this operation.
- First, create an instance of the Presentation class to create a new or load an existing PowerPoint presentation.
- Get the reference of the slide into an ISlide object.
- Get the shape collection into IShapeCollection object using Presentation.getSlides().get_Item(0).getShapes() method.
- Add clone of a shape using IShapeCollection.addClone(sourceShapes[int]) method.
- Finally, save the presentation using Presentation.save(String, SaveFormat.Pptx) method.
The following code sample shows how to add clone of a PowerPoint shape using Java.
Remove Shapes from PowerPoint Slides in Java
You can also remove the PowerPoint shapes from a slide using Aspose.Slides for Java. The following are the steps to remove a PowerPoint shape from the shape collection.
- First, create an instance of Presentation class to load a PowerPoint presentation.
- Get the reference of the desired slide into an ISlide object.
- Remove particular shape by index using ISlide.getShapes().removeAt(int) method.
- Finally, save the presentation using Presentation.save(String, SaveFormat.Pptx) method.
The following code sample shows how to remove a PowerPoint shape from a slide using Java.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to work with shapes in PowerPoint presentations using Java. Particularly, you have seen how to add, connect, remove or clone the PowerPoint shapes. Furthermore, you can explore more about the API by consulting its documentation. Also, in case of any queries, feel free to let us know via our forum.