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();
}
}