Group Shapes in PowerPoint in Java

In PowerPoint, if you want to add a shape that is not included in the default shape library, you can group shapes to create custom shapes that meet your requirements. In this article, you will learn how to programmatically group shapes in a PowerPoint document using Free Spire.Presentation for Java.

Import JAR Dependency (2 Methods)

● You can also add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Sample Code

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class GroupShapes {
public static void main(String[] args) throws Exception {
//Create a PowerPoint document
Presentation ppt = new Presentation();
//Get the first slide
ISlide slide = ppt.getSlides().get(0);

//Add a rectangle shape to the slide
IShape rectangle = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(20,100,200,40));
rectangle.getFill().setFillType(FillFormatType.SOLID);
rectangle.getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_YELLOW);
rectangle.getLine().setWidth(0.1f);

//Add a ribbon shape to the slide
IShape ribbon = slide.getShapes().appendShape(ShapeType.RIBBON_2, new Rectangle2D.Double(60, 75, 120, 80));
ribbon.getFill().setFillType(FillFormatType.SOLID);
ribbon.getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);
ribbon.getLine().setWidth(0.1f);

//Add the shapes to a list
ArrayList list = new ArrayList();
list.add((Shape)rectangle);
list.add((Shape)ribbon);

//Group the shapes
ppt.getSlides().get(0).groupShapes(list);

//Save the resultant document
ppt.saveToFile("GroupShapes.pptx", FileFormat.PPTX_2013);
}
}

--

--

Sharing Java Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store