DEV Community

CodeSharing
CodeSharing

Posted on

Add Shapes to PowerPoint using Java

This article will demonstrate how to add a variety of shapes to a PowerPoint slide and how to fill the shapes with a solid color, a picture, a pattern or a gradient using Java.

Tools:
Free Spire.Presentation for Java
● IntelliJ IDEA

Installation(2 Methods)
● Download the free Java API and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

● 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>3.9.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Java Code

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

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class AddShapes {

    public static void main(String[] args) throws Exception {

        //create a PowerPoint document
        Presentation presentation = new Presentation();

        //append a Triangle and fill the shape with a solid color
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(115, 130, 100, 100));
        shape.getFill().setFillType(FillFormatType.SOLID);
        shape.getFill().getSolidColor().setColor(Color.orange);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append an Ellipse and fill the shape with a picture
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.ELLIPSE, new Rectangle2D.Double(290, 130, 150, 100));
        shape.getFill().setFillType(FillFormatType.PICTURE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        BufferedImage image = ImageIO.read(new File("logo.jpg"));
        shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Heart and fill the shape with a pattern
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.HEART, new Rectangle2D.Double(515, 130, 130, 100));
        shape.getFill().setFillType(FillFormatType.PATTERN);
        shape.getFill().getPattern().setPatternType(PatternFillType.CROSS);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Five-Pointed Star and fill the shape with a gradient color
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.FIVE_POINTED_STAR, new Rectangle2D.Double(115, 300, 100, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.BLACK);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Rectangle and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(290, 300, 150, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.LIGHT_SKY_BLUE);
        shape.getFill().getGradient().getGradientStops().append(1, KnownColors.ROYAL_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //append a Bent Up Arrow and fill the shape with gradient colors
        shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.BENT_UP_ARROW, new Rectangle2D.Double(515, 300, 130, 100));
        shape.getFill().setFillType(FillFormatType.GRADIENT);
        shape.getFill().getGradient().getGradientStops().append(1f, KnownColors.OLIVE);
        shape.getFill().getGradient().getGradientStops().append(0, KnownColors.POWDER_BLUE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);

        //save the document
        presentation.saveToFile("output/AddShapes.pptx", FileFormat.PPTX_2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (3)

Collapse
 
pmgysel profile image
Philipp Gysel

Great article! So I had a look at the other products offered by Spice and it looks like the cover a lot, including PDF generation, bar code generation and much more. Do you know how the licensing works and what is free?

Collapse
 
codesharing profile image
CodeSharing

They have a list of products like Spire.Doc, Spire.PDF, Spire.XLS, Spire.Presentation that are used to manipulate documents in different formats on .NET or Java platforms. Only the commercial version needs to apply a license to remove the evaluation message on the generated documents, while the free version doesn't need that and there's no evaluation message, but free veison has certain page limitations.

Collapse
 
codesharing profile image
CodeSharing

A tutorial about licensing:
e-iceblue.com/Tutorials/Licensing/...