前言
PPT母版具有强大的模板作用,它能够允许用户根据自己的需求自行设计幻灯片的标题文字、背景图、主题颜色等。PPT母版设计成功后可以直接调用此模板应用到其他幻灯片,避免多次重复编辑。本文将介绍如何使用代码在Java应用程序中创建幻灯片母版样式并应用到其他幻灯片。
基本步骤
1. 下载Free Spire.Presentation for Java包并解压缩。
2. 将lib文件夹下的Spire.Presentation.jar包作为依赖项导入到Java应用程序中。(也可直接通过Maven仓库安装JAR包(配置pom.xml文件的代码见下文))
3. 在Java应用程序中新建一个Java Class(此处我命名为CreateSlideMaster 和 CreateMultiSlideMasters),然后输入相应的Java代码并运行。
配置pom.xml文件:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
创建唯一母版
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;
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 = "pic.jpg";
String logo = "logo.jpg";
//设置母版背景
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);
//添加图片到母版
image = ImageIO.read(new FileInputStream(logo));
imageData = presentation.getImages().append(image);
IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(60,60,220,80));
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,60));//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(20f);
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();
}
}
创建多个母版并分别应用到幻灯片
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.image.BufferedImage;
import java.io.FileInputStream;
public class CreateMultiSlideMasters {
public static void main(String[] args) throws Exception {
//新建PPT文档
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//插入4页幻灯片(连同默认的幻灯片,文档中共5页)
for (int i = 0; i < 4; i++)
{
presentation.getSlides().append();
}
//获取默认的母版
IMasterSlide first_master = presentation.getMasters().get(0);
//创建并获取第二个母板
presentation.getMasters().appendSlide(first_master);
IMasterSlide second_master = presentation.getMasters().get(1);
//为两个母版分别设置不同的背景图片
String pic1 = "image1.jpg";
String pic2 = "image2.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(pic1));
IImageData imageData = presentation.getImages().append(image);
first_master.getSlideBackground().setType(BackgroundType.CUSTOM);
first_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
first_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
first_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
image = ImageIO.read(new FileInputStream(pic2));
imageData = presentation.getImages().append(image);
second_master.getSlideBackground().setType(BackgroundType.CUSTOM);
second_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
second_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
second_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
//在第一页应用第一个母版及版式(板式6为空板式)
presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));
//在剩下的幻灯片应用第二个母版及版式
for (int i = 1; i < presentation.getSlides().getCount(); i++)
{
presentation.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
}
//保存文档
presentation.saveToFile("MultiSlideMaters.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}