Java 创建并应用PPT幻灯片母版的方法示例
程序员文章站
2022-11-01 14:08:53
前言在powerpoint文档中,幻灯片母版可供用户设置幻灯片的样式,比如标题文字、背景、属性等。预先设定好的幻灯片母版可用于所有幻灯片,此外,也可创建多个幻灯片母版分别应用到幻灯片中。本文将介绍如何...
前言
在powerpoint文档中,幻灯片母版可供用户设置幻灯片的样式,比如标题文字、背景、属性等。预先设定好的幻灯片母版可用于所有幻灯片,此外,也可创建多个幻灯片母版分别应用到幻灯片中。本文将介绍如何创建并应用单个或多个幻灯片母版。
环境构建
文中演示代码用到的工具是free spire.presentation for java,可通过官网下载获取。解压后将位于lib文件夹下的spire.presentation.jar导入java程序。此外,还可通过。
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(); } }
创建效果:
到此这篇关于java 创建并应用ppt幻灯片母版的方法示例的文章就介绍到这了,更多相关java 创建ppt幻灯片母版内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!