Java 创建并应用幻灯片母版
1、步骤一:下载安装好后,解压,将解压后的文件夹下的子文件夹lib 中的jar包导入到project中。

2、步骤二:添加jar包,完成引用。

1、import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CreateSlideMaster {
public static void main(String[] args) throws Exception {
//创建PPT文档,指定幻灯片大小
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//获取第一张母版
IMasterSlide masterSlide = presentation.getMasters().get(0);
//获取图片地址
String backgroundPic = "C:/Users/Administrator/Desktop/纯色.jpg";
String logo = "C:/Users/Administrator/Desktop/logo.png";
//设置母版背景
BufferedImage image = ImageIO.read(new FileInputStream(backgroundPic));
IImageData imageData = presentation.getImages().append(image);
masterSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
masterSlide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
masterSlide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
masterSlide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
//添加图片(公司Logo)到母版
image = ImageIO.read(new FileInputStream(logo));
imageData = presentation.getImages().append(image);
IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(40,40,200,60));
imageShape.getLine().setFillType(FillFormatType.NONE);
//添加文字(公司名称)到母版
IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) presentation.getSlideSize().getSize().getWidth()-200,(float) presentation.getSlideSize().getSize().getHeight()-60,200,30));//Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(ppt.SlideSize.Size.Width-200, ppt.SlideSize.Size.Height-60, 200, 30));
textShape.getTextFrame().setText("岁月悠悠,思君令人老");
textShape.getTextFrame().getTextRange().setFontHeight(15f);
textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.blue);
textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
textShape.getFill().setFillType(FillFormatType.NONE);
textShape.getLine().setFillType(FillFormatType.NONE);
//添加一张幻灯片
presentation.getSlides().append();
//保存文档
presentation.saveToFile("output/SlideMaster.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
1、完成代码后,运行程序,效果如下:
