前言
在 PowerPoint 文档中,幻灯片母版可供用户设置幻灯片的样式,比如标题文字、背景、属性等。预先设定好的幻灯片母版可用于所有幻灯片,此外,也可创建多个幻灯片母版分别应用到幻灯片中。本文将介绍如何创建并应用单个或多个幻灯片母版。
环境构建
文中演示代码用到的工具是 Free Spire.Presentation for Java,可通过官网下载获取。解压后将位于 lib 文件夹下的 Spire.Presentation.jar 导入 Java 程序。此外,还可通过 maven 仓库安装导入。
Java 代码示例
示例 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;
public class UniqueSlideMaster {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\\Test1\\Desktop\\Background.jpg";
String logo = "C:\\Users\\Test1\\Desktop\\logo2.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,100));
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();}
}
创建效果:
示例 2 创建多个母版并分别应用到幻灯片
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 MultiSlideMasters {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 = "C:\\Users\\Test1\\Desktop\\Image1.jpg";
String pic2 = "C:\\Users\\Test1\\Desktop\\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("output/MultiSlideMaters.pptx", FileFormat.PPTX_2013);
presentation.dispose();}
}
创建效果:
(本文完)