Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created April 9, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/0e0e4646fe91c0791330665370d31edb to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0e0e4646fe91c0791330665370d31edb to your computer and use it in GitHub Desktop.
Work with PowerPoint Shapes in Java
// Instantiate Presentation class that represents the PPTX
Presentation pres = new Presentation();
// Get the first slide
ISlide sld = pres.getSlides().get_Item(0);
// Add AutoShape of ellipse type
sld.getShapes().addAutoShape(ShapeType.Ellipse, 100, 150, 150, 100);
// Write the PPTX file to disk
pres.save("add_shape.pptx", SaveFormat.Pptx);
// Instantiate Presentation class that represents the PPTX
Presentation pres = new Presentation();
// Get the first slide
ISlide sld = pres.getSlides().get_Item(0);
// Add AutoShape of rectangle type
IShape shp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 75, 150);
// Rotate the shape to 90 degree
shp.setRotation(90);
// Write the PPTX file to disk
pres.save("clone_shape.pptx", SaveFormat.Pptx);
// Instantiate Presentation class that represents the PPTX file
Presentation input = new Presentation();
// Access shapes collection for selected slide
IShapeCollection shapes = input.getSlides().get_Item(0).getShapes();
// Add Autoshape Ellipse
IAutoShape ellipse = shapes.addAutoShape(ShapeType.Ellipse, 0, 100, 100, 100);
// Add Autoshape Rectangle
IAutoShape rectangle = shapes.addAutoShape(ShapeType.Rectangle, 100, 300, 100, 100);
// Add connector shape to slide shape collection
IConnector connector = shapes.addConnector(ShapeType.BentConnector2, 0, 0, 10, 10);
// Join Shapes to connectors
connector.setStartShapeConnectedTo(ellipse);
connector.setEndShapeConnectedTo(rectangle);
connector.reroute();
// Save Presentation
input.save("connect_shape.pptx", SaveFormat.Pptx);
// Load presentation
Presentation presentation1 = new Presentation("clone_shape.pptx");
// Get slide
ISlide slide = presentation1.getSlides().get_Item(0);
// Shape text to find the shape
String alttext = "User Defined";
// Get number of shapes
int iCount = slide.getShapes().size();
// Loop through shapes
for (int i = 0; i < iCount; i++) {
// Get shape
IAutoShape ashp = (IAutoShape) slide.getShapes().get_Item(0);
if (ashp.getAlternativeText().compareTo(alttext) == 0) {
// Remove shape
slide.getShapes().remove(ashp);
}
}
presentation1.save("remove_shape.pptx", SaveFormat.Pptx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment