本教程展示如何使用Spire.Presentation for Java在PowerPoint文档中添加各种形状,以及如何对形状进行单色填充、渐变色填充、图片填充和图案填充。
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 {
//创建PowerPoint文档
Presentation presentation = new Presentation();
//添加一个三角形,并设置单色填充
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);
//添加一个椭圆,并设置图片填充
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\\Administrator\\Desktop\\logo.png"));
shape.getFill().getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(image));
shape.getShapeStyle().getLineColor().setColor(Color.white);
//添加一个心形,并设置图案填充
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);
//添加一个五角星,并设置渐变填充
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);
//添加一个矩形,并设置渐变填充
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);
//添加一个向上的箭头,并设置渐变填充
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);
//保存文档
presentation.saveToFile("AddShapes.pptx", FileFormat.PPTX_2010);
}
}