· 

Java – Fill a Shape with Color, Picture or Pattern

Shape is the best way to make your presentations more interesting. In this article, I am going to introduce how to add basic shapes (rectangle, triangle, circle, etc.) to a PowerPoint slide, and how to fill a shape with color, a picture or a pattern, by using Spire.Prensentation for Java. 

Installing Spire.Presentation.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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</artifactId>
        <
version>4.4.2</version>
    </
dependency>
</
dependencies>

Using the 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 AddShapesToSlide {

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

       
//Create a Presentation object
       
Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.
SCREEN_16_X_9);

       
//Add a rectangle and fill the shape with a solid color
       
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(200, 100, 200, 100));
        shape.getFill().setFillType(FillFormatType.
SOLID);
        shape.getFill().getSolidColor().setColor(Color.
orange);
        shape.getShapeStyle().getLineColor().setColor(
new Color(1,1,1,0));

       
//Add a five-pointed star and fill the shape with a pattern
       
shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.FIVE_POINTED_STAR, new Rectangle2D.Double(550, 50, 180, 180));
        shape.getFill().setFillType(FillFormatType.
PATTERN);
        shape.getFill().getPattern().setPatternType(PatternFillType.
CROSS);
        shape.getShapeStyle().getLineColor().setColor(
new Color(1,1,1,0));

       
//Add a triangle and fill the shape with a picture
       
shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.TRIANGLE, new Rectangle2D.Double(150, 300, 140, 140));
        shape.getFill().setFillType(FillFormatType.
PICTURE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.
STRETCH);
        BufferedImage image = ImageIO.read(
new File("C:\\Users\\Administrator\\Desktop\\logo.png"));
        shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
        shape.getShapeStyle().getLineColor().setColor(
new Color(1,1,1,0));

       
//Add a ellipse and fill the shape with a radial gradient
       
shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.ELLIPSE, new Rectangle2D.Double(440, 320, 120, 120));
        shape.getFill().setFillType(FillFormatType.
GRADIENT);
        shape.getFill().getGradient().setGradientShape(GradientShapeType.
RADIAL);
        shape.getFill().getGradient().getGradientStops().append(
0, KnownColors.LIGHT_PINK);
        shape.getFill().getGradient().getGradientStops().append(
1,KnownColors.DEEP_PINK);
        shape.getFill().getGradient().setGradientStyle(GradientStyle.
FROM_CENTER);
        shape.getShapeStyle().getLineColor().setColor(
new Color(1,1,1,0));

       
//Append a Rectangle and fill the shape with a linear gradient
       
shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.CLOUD, new Rectangle2D.Double(680, 320, 180, 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(
new Color(1,1,1,0));

       
//Save the document
       
presentation.saveToFile("output/AddShapes.pptx", FileFormat.PPTX_2010
);
    }
}

Write a comment

Comments: 0