PowerPoint中的母版保存了一些固定样式,例如背景图、标题、主题颜色等,能被其它幻灯片所继承。本文介绍如何使用Spire.Presentation for Java创建母版样式并应用到其他幻灯片。
创建唯一母版
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 = "C:/Users/Administrator/Desktop/bg.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();
}
}
创建多个母版并分别应用到幻灯片
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 = "C:/Users/Administrator/Desktop/image1.png";
String pic2 = "C:/Users/Administrator/Desktop/image2.png";
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();
}
}