本教程展示如何使用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);
}
}