Shapes
in PowerPoint can be used to add interest to any presentation, emphasize a point
or create custom graphics based on your requirement. Generally, shapes used in
PowerPoint are circles, rectangles, squares, pentagons and so on. Furthermore, individual
shapes can be merged in to other complex shapes. Let’s take a look at how to
add individual shapes or group shapes to PowerPoint by using Free
Spire.Presentation for Java.
ADD SPIRE.PRESENTATION.JAR AS
DEPENDENCY
Download the latest version of Free Spire.Presentation for Java from the link, unzip it
and add Spire.Presentation.jar located in the “lib” folder to your Java IDEA.
Of course, if you are creating a Maven project, you can easily add the jar dependency 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</artifactId>
<version>3.5.4/version>
</dependency>
</dependencies>
Using the code
Sample 1 Add Shapes
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 AddShape {
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("C:\\Users\\Test1\\Desktop\\Image.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 color
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 color
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);
}
}
Output
Sample 2 Add Group shapes
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
public class GroupShape {
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.GOLD);
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.PURPLE);
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("output/GroupShapes.pptx",
FileFormat.PPTX_2013);
}
}
Output
No comments:
Post a Comment