欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 创建并应用PPT幻灯片母版

程序员文章站 2022-03-20 15:37:45
幻灯片母版,可在幻灯片中预先存储设计模板信息,包括字形、占位符大小或位置、背景设计和配色方案等;对设定好的母版可应用于所有幻灯片,也可设计多个不同母版应用于不同幻灯片。下面通过Java代码示例介绍如何创建单一母版以及不同母版。 使用工具:Free Spire.Office for Java(免费版) ......

幻灯片母版,可在幻灯片中预先存储设计模板信息,包括字形、占位符大小或位置、背景设计和配色方案等;对设定好的母版可应用于所有幻灯片,也可设计多个不同母版应用于不同幻灯片。下面通过java代码示例介绍如何创建单一母版以及不同母版。

使用工具:free spire.office for java(免费版)

jar获取及导入:官网下载jar包,并解压将lib文件夹下的jar文件导入java程序,或者通过

如下导入效果:

Java 创建并应用PPT幻灯片母版

 

 

 

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 createmasterslide {
    public static void main(string[] args) throws exception {
        //创建ppt文档,并设置幻灯片大小
        presentation ppt = new presentation();
        ppt.getslidesize().settype(slidesizetype.screen_16_x_9);


        //获取第一张母版
        imasterslide masterslide = ppt.getmasters().get(0);

        //设置母版背景
        bufferedimage image = imageio.read(new fileinputstream("tp.png"));
        iimagedata imagedata = ppt.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.png"));
        imagedata = ppt.getimages().append(image);
        iembedimage imageshape = masterslide.getshapes().appendembedimage(shapetype.rectangle,imagedata,new rectangle2d.float((float) ppt.getslidesize().getsize().getwidth()-240,40,60,60));
        imageshape.getline().setfilltype(fillformattype.none);

        //添加文字到母版
        iautoshape textshape = masterslide.getshapes().appendshape(shapetype.rectangle, new rectangle2d.float((float) ppt.getslidesize().getsize().getwidth()-230,85,200,30));
        textshape.gettextframe().settext("文娱传媒");
        textshape.gettextframe().gettextrange().setfontheight(20f);
        textshape.gettextframe().gettextrange().getfill().setfilltype(fillformattype.solid);
        textshape.gettextframe().gettextrange().getfill().getsolidcolor().setcolor(color.black);
        textshape.gettextframe().gettextrange().getparagraph().setalignment(textalignmenttype.center);
        textshape.getfill().setfilltype(fillformattype.none);
        textshape.getline().setfilltype(fillformattype.none);

        //添加一张幻灯片(创建ppt文档时,已默认生成一张幻灯片,这里添加一张幻灯片可对比查看母版添加效果)
        ppt.getslides().append();

        //保存文档
        ppt.savetofile("createslidemaster.pptx", fileformat.pptx_2013);
        ppt.dispose();
    }
}

母版创建效果:

Java 创建并应用PPT幻灯片母版

 

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.*;
import java.awt.geom.rectangle2d;
import java.awt.image.bufferedimage;
import java.io.fileinputstream;

public class createmasterslide2 {
    public static void main(string[] args) throws exception{
        //创建ppt文档,并设置幻灯片大小
        presentation ppt = new presentation();
        ppt.getslidesize().settype(slidesizetype.screen_16_x_9);

        //插入4页幻灯片(连同默认的幻灯片,文档*5页)
        for (int i = 0; i < 4; i++)
        {
            ppt.getslides().append();
        }

        //获取默认的母版
        imasterslide first_master = ppt.getmasters().get(0);

        //创建并获取第二个母板
        ppt.getmasters().appendslide(first_master);
        imasterslide second_master = ppt.getmasters().get(1);

        //为两个母版分别设置不同的背景图片
        bufferedimage image = imageio.read(new fileinputstream("pic1.png"));
        iimagedata imagedata = ppt.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);
        iautoshape textshape = first_master.getshapes().appendshape(shapetype.rectangle, new rectangle2d.float((float) ppt.getslidesize().getsize().getwidth()/3,180,200,30));
        textshape.gettextframe().settext("首页母版");
        textshape.gettextframe().gettextrange().setfontheight(40f);
        textshape.gettextframe().gettextrange().getfill().setfilltype(fillformattype.solid);
        textshape.gettextframe().gettextrange().getfill().getsolidcolor().setcolor(color.red);
        textshape.gettextframe().gettextrange().getparagraph().setalignment(textalignmenttype.center);
        textshape.getfill().setfilltype(fillformattype.none);
        textshape.getline().setfilltype(fillformattype.none);

        image = imageio.read(new fileinputstream("pic2.png"));
        imagedata = ppt.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为空板式)
        ppt.getslides().get(0).setlayout(first_master.getlayouts().get(6));

        //在剩下的幻灯片应用第二个母版及版式
        for (int i = 1; i < ppt.getslides().getcount(); i++)
        {
            ppt.getslides().get(i).setlayout(second_master.getlayouts().get(6));
        }

        //保存文档
        ppt.savetofile("multislidematers.pptx", fileformat.pptx_2013);
        ppt.dispose();
    }
}

多个母版创建效果:

Java 创建并应用PPT幻灯片母版

 

(完)